lvRet: string;
// lvbytes:TArray<Byte>;
begin
// lvbytes:=Gen1MBytes();
//if length(lvbytes)>0 then
// SetLength(lvBuffer,1024*1024);
try
// lvBuffer[1]:='A';
// lvBuffer[1024*1024]:='B';
lvBuffer := Gen1MStr();
t := GetTickCount;
for i := 1 to 100 do
begin
lvRet := StrToHex(lvBuffer);
end;
t := GetTickCount - t;
Memo1.Lines.Add('StrToHex:' + formatfloat('###,###0.00M/S', 100 / t * 1000));
t := GetTickCount;
for i := 1 to 100 do
begin
lvBuffer := HexToStr(lvRet);
end;
t := GetTickCount - t;
Memo1.Lines.Add('HexToStr:' + formatfloat('###,###0.00M/S', 100 / t * 1000));
SetLength(lvBuffer, 1024 * 1024);
t := GetTickCount;
for i := 1 to 100 do
begin
lvRet := TNetEncoding.Base64.EncodeBytesToString(PAnsiChar(lvBuffer),
Length(lvBuffer));
end;
t := GetTickCount - t;
Memo1.Lines.Add('Base64 Encode:' + formatfloat('###,###0.00M/S', 100 / t * 1000));
lvBuffer := lvRet;
t := GetTickCount;
for i := 1 to 100 do
begin
TNetEncoding.Base64.DecodeStringToBytes(lvBuffer);
end;
t := GetTickCount - t;
Memo1.Lines.Add('Base64 Dencode:' + formatfloat('###,###0.00M/S', 100 / t * 1000));
finally
// FreeMem(lvBuffer,1024*1024);
end;
end;
function StrToHex(Value: AnsiString): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(Value) do
Result := Result + IntToHex(Ord(Value[i]), 2);
end;
// 16׆èؖػԮéתؖػԮ
function HexToStr(Value: AnsiString): AnsiString;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(Value) do
begin
if ((i mod 2) = 1) then
Result := Result + ansichar(StrToInt('0x' + Copy(Value, i, 2)));
end;
end;
function Gen1MBytes(pvSize:integer=1024*1024):TArray<Byte>;
var lvBuffer:PByteArray;
begin
GetMem(lvBuffer,pvSize);
TRY
SetLength(result,pvSize);
Move(lvbuffer^,result[0],pvSize);
FINALLY
FreeMem(lvBuffer);
END;
end;
function Gen1MStr(): AnsiString;
var
lvStream: TFileStream;
begin
SetLength(Result, 1024 * 1024);
lvStream := TFileStream.Create('c:\windows\system32\nvcompiler.dll', fmOpenRead);
try
lvStream.ReadBuffer(Result[1],1024*1024);
finally
FreeAndNil(lvStream);
end;
end;
在老y460笔记本上对比结果:
StrToHex: 6.9M/S
HexToStr: 4.19M/S
Base64 Encode: 156.25M/S
Base64 Decode: 65.4M/S
相差10倍以上, 不过,strtohex函数存在很大的优化空间,但即使优化得再好,跟base64亦难以匹敌。