字串转16进制、转base64在速度对比。。。



 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亦难以匹敌。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值