在低延迟HFT
应用程序中,我有套接字接收包含股票市场数据的Byte[]
消息。
在供应商协议中,股票价格字段为Byte[]
,其中包含ASCII
个字符序列
即字节序列[51-51-46-56]对应ascii字符[3-3 -.- 8],等于33.8数字。
实际上在消息解析期间,我将Byte[]
转换为字符串,然后将字符串转换为Single / float。
有没有办法避免这种双重转换并直接将Byte[]
转换为单个/浮动?
其次有没有办法避免使用在.NET中效率低下的字符串类型(容易发生不可变垃圾收集)?
提前致谢。
此致
[编辑2019年]:这是3年来完美无缺的最终解决方案:
/// <summary> Read a Double precision float field in GL format from the stream and advances stream position of n (1 of field lenght flag + field lenght). </summary>
/// <returns>-1 in case of error, Double precision floating point value ROUNDED TO 8 DECIMAL PLACES in case of success </returns>
/// <param name="IniPos"> Initial Stream Position</param>
public double ReadGLFieldAsDouble(long IniPos = -1)
{
// --
if (IniPos >= 0)
Strm.Position = IniPos;
int FLenght = Strm.ReadByte - 32; // case of "01000" SW Snapshot, field 7 doesn't exists, but in the message is a blank (32)
// --early exit
if (FLenght <= 0)
return -1; // a blank field (byte 32) returns -1
if (Strm.Length - Strm.Position < FLenght)
return -1;
// --
double Dbl = 0;
int Cpos = -1;
int b = 0;
int sign = 1;
// --Builds as Integer with no point Separator
for (int i = 0; i < FLenght ; i++)
{
b = Strm.ReadByte;
switch (b)
{
case 45: // ASCII 45="-" sign
{
sign = -1;
break;
}
case 46: // ASCII 46 is decimal point="." ; ASCII 44 is comma=","
{
Cpos = i; // decimal point position
break;
}
default:
{
Dbl = Dbl * 10 + (b - 48); // increments as integer ASCII 48=0
break;
}
}
}
// --Transforms in floating point dividing by power of 10, multiplies by sign and returns
if (Cpos != -1)
Dbl = (Dbl / (Math.Pow(10, (FLenght - 1 - Cpos))));
return Math.Round(sign * Dbl, 8);
}
答案 0 :(得分:6)
byte[] arr = new byte[] { 51, 51, 46, 56};
double res = 0, floatindex = 0.1;
bool isFraction = false;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 46)
{
isFraction = true;
continue;
}
if (!isFraction)
res = 10*res + arr[i] - 48;
else
{
res += (arr[i] - 48)*floatindex;
floatindex /= 10.0;
}
}
return res;
从文化的角度来看,它假设你总是有一个点'&#39;标记小数点分隔(例如,不像法国文化中的昏迷)
答案 1 :(得分:1)
有可能。假设您知道格式总是那样(数字,逗号和十进制数字):
float v = 0.1f *(100.0f *(buf [0] - 48)+ 10 *(buf [1] - 48)+(buf [3] - 48));
或: float v = 0.1f *(100.0f * buf [0] + 10 * buf [1] + buf [3] - 4800 - 480 - 48);