校验和算法的并行优化?
下面的代码示例是我在一个项目中使用的 CRC-CCITT 的实现。
public static unsafe ushort CRC(byte * it, byte * end)
{
unchecked
{
ushort crc = 0xFFFF;
ushort quick = 0;
for (;;)
{
ushort tmp = (ushort)((crc >> 8) ^ (*it));
crc <<= 8;
quick = (ushort)(tmp ^ (tmp >> 4));
crc ^= quick;
quick <<= 5;
crc ^= quick;
quick <<= 7;
crc ^= quick;
if (it == end)
break;
it++;
}
return crc;
}
}
CRC-CCITT 使用以下多项式公式:
(X^16 + X^12 + X^5 + 1)
问:上述多项式只不过是一系列加/乘运算。数学的基本定律规定加法/乘法运算是可以互换的等,因此表达式如下:
SUM(from 1 to 10) == SUM(from 1 to 5) + SUM(from 6 to 10)
是真的。
我需要优化上面的代码,它可能是我的项目中最常调用的东西(至少120次/秒)。考虑到上述情况,这可以通过 CRC 校验和实现吗?我正在考虑使用 Parallel.For(...) 来解决这个问题,这有意义吗?有人有什么建议吗?
更新:
实际上每个连接 120 次。我正在处理至少 15 个同步传入连接,数据速率为 120[Hz] 等。字节数组可能会有所不同 - 理论最大值 = 65k 字节,但这种情况很少见,最常见的是大约 1k 字节。
The below code sample is an implementation of CRC-CCITT that I'm using in one of my projects.
public static unsafe ushort CRC(byte * it, byte * end)
{
unchecked
{
ushort crc = 0xFFFF;
ushort quick = 0;
for (;;)
{
ushort tmp = (ushort)((crc >> 8) ^ (*it));
crc <<= 8;
quick = (ushort)(tmp ^ (tmp >> 4));
crc ^= quick;
quick <<= 5;
crc ^= quick;
quick <<= 7;
crc ^= quick;
if (it == end)
break;
it++;
}
return crc;
}
}
The CRC-CCITT uses the following polynominal formula :
(X^16 + X^12 + X^5 + 1)
Q: The above polynominal is nothing more then a series of add/multiplication operations. The basic laws of mathematics state that add/multiply ops are interchangeable etc. so expressions like :
SUM(from 1 to 10) == SUM(from 1 to 5) + SUM(from 6 to 10)
are true.
I need to optimize the above code, it is probably the most frequently called thing in my project, (120 times/sec at least). Having considered the above, would this be doable with a CRC checksum ? I'm considering using Parallel.For(...) to do the trick, does that even make sense? Anyone have any suggestions?
Update :
120 times per connection actually. I'm handling at least 15 simultaneous incoming connections with datarates of 120[Hz] etc. Byte arrays can vary - theoretical max = 65k bytes, but that's rarely the case, most often it's circa 1k bytes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这能解决您的问题吗?
(抱歉,我不知道如何避免已删除的帖子!)
Could this solve your problem?
(Sorry, I don't know how to circumvent a deleted post!)