方法:function Encode64(const test:string): string;
test量大时算法效率很低,需要几分钟才运算完function Encode64(const test:string): string;
var
  s, s1: string;
  i, p, len, n, Addnum: integer;begin
  result := '';  S := '';
  for i := 1 to length(test) do
    S := S + IntToHex(Ord(test[I]), 2);
  //将字符串的十六进制数以字符串的形式表示出来
  case (length(s) mod 3) of
    0: addnum := 0;
    1:
    begin
      s := s + '00';
      addnum := 2;
    end;
    2:
    begin
      s := s + '0';
      addnum := 1;
    end;
    else
      addnum := 0;
  end;
  len := length(s) div 3;
  for i := 1 to len do
  begin
    s1 :=Midstr(S, i*3-2,3);
    p := strtoint('$' + s1);
    n := p div 64;
    result := result + basetable[n+1];
    n := p mod 64;
    result := result + basetable[n+1];
  end;
  if addnum = 1 then
    result := result + '==';
  if addnum = 2 then
    result[length(result)] := '=';
end;

解决方案 »

  1.   

    如果指定了用这个算法,那就对这个算法作优化,优化如下:
    1.给S,Result字符串预设长度,并用Move,或者PWORD^/PDWORD^进行操作
    2.用PChar来循环字符串
    3.针对该算法专门写一个IntToHex2,StrToInt2,以及Midstr(这个其实可以不要),来提交速度理论上,100M的数据,这个算法,2秒内应该可以完成,内存足够的话,100M test数据需要400M左右的内存
      

  2.   

    几分钟有点夸张吧。  自带就有这个函数, 在EncdDecd单元里。  function  EncodeString(const Input: string): string; //加密
    function  DecodeString(const Input: string): string; //解密
      

  3.   

    从你的算法来看:
    S := '';
    for i := 1 to length(test) do
      S := S + IntToHex(Ord(test[I]), 2); //只取了2位,因此只支持AnsiString
    这是一个专为AnsiString而设计的函数.
    另外楼主的函数和EncodeString的结果不一致,比如字符串长度>=56的时候.
    100M AnsiString用EncodeString的时间大概1秒左右.应该是可以接受的,而且这个函数是标准函数.加和解都没问题.(D7,Win7,i5,4G环境测试的)另:发一个优化后的代码给你,是完全根据你的函数改写的,结果只和你的函数结果一致.仅作为代码算法研究参考,建议用系统自带的那2个函数.
    100M AnsiString执行时间0.23秒,同样只能使用AnsiString
    function Encode64Fast(const Input : AnsiString): AnsiString;
    var
      pDest , pSour : PChar;
      i , Count : integer;
      w : DWORD;
      B : Boolean;
    begin
      if Input='' then begin
        Result := '';
        exit;
      end;
      SetLength(Result , Length(Input)*3 DIV 2 + 100);//预设长度
      pDest := Pointer(Result);
      pSour := Pointer(Input);
      Count := 0;
      B := False;
      for i:=1 to (Length(Input)+1)*2 DIV 3 do begin  //计算部分
        w := ((PByte(pSour)^ SHL 8) or (PByte(pSour+1)^));//不支持BSWAP,因此只能这样了
        inc(pSour);
        if not B then w := w SHR 4
        else begin
          w := w AND $0FFF;
          inc(pSour);
        end;
        B := Not B;    pDest^ := BaseTable[(w SHR 6 ) + 1];  //代替 DIV 64
        inc(pDest);
        pDest^ := BaseTable[(w and 63) + 1];  //代替 MOD 64
        inc(pDest);
        inc(Count , 2);
      end;  case (Length(Input)*2 mod 3) of
        1: (pDest-1)^ := '=';
        2: begin
             pDest^ := '=';
             inc(pDest);
             pDest^ := '=';
             inc(Count , 2);
           end;
      end;
      Delete(Result , Count+1 , Length(Result));
    end;