问题描述
我需要在大型文件上使用$ 1021多项式来计算Crc16校验和,以下是我当前的实现,但是在大型文件上它的运行速度相当慢(例如,一个90 MB的文件大约需要9秒钟)。 b $ b
所以我的问题是如何改进当前的实现(使其更快),我用谷歌搜索了一些实现表查找的示例,但是我的问题是我不知道如何将它们修改为包括多项式(可能是我的数学失败了。)
{根据http://miscel.dk/MiscEl/CRCcalculations。 html}
函数Crc16(const Buffer:PByte; const BufSize:Int64;
const Polynom:WORD = $ 1021; const Seed:WORD = 0):Word;
var
i,j:整数;
开始
结果:=种子;
for i:= 0到BufSize-1做
开始
结果:=结果xor(Buffer [i] shl 8);如果(结果和$ 8000)<<> ;,则j:= 0至7的
确实开始
。 0然后
结果:=(结果shl 1)xor多项式
else结果:=结果shl 1;
结尾;
结尾;
结果:=结果和$ FFFF;
结尾;
从Jedi的jclMath.pas单元中查找CRC例程代码库。它使用CRC查找表。
I need to calculate Crc16 checksums with a $1021 polynom over large files, below is my current implementation but it's rather slow on large files (eg a 90 MB file takes about 9 seconds).
So my question is how to improve my current implementation (to make it faster), I have googled and looked at some samples implementing a table lookup but my problem is that I don't understand how to modify them to include the polynom (probably my math is failing).
{ based on http://miscel.dk/MiscEl/CRCcalculations.html }
function Crc16(const Buffer: PByte; const BufSize: Int64;
const Polynom: WORD=$1021; const Seed: WORD=0): Word;
var
i,j: Integer;
begin
Result := Seed;
for i:=0 to BufSize-1 do
begin
Result := Result xor (Buffer[i] shl 8);
for j:=0 to 7 do begin
if (Result and $8000) <> 0 then
Result := (Result shl 1) xor Polynom
else Result := Result shl 1;
end;
end;
Result := Result and $FFFF;
end;
Look for CRC routines from jclMath.pas unit of Jedi Code Library. It uses CRC lookup tables.
http://jcl.svn.sourceforge.net/viewvc/jcl/trunk/jcl/source/common/
这篇关于提高Crc16计算速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!