随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

问题来源: http://www.cnblogs.com/del/archive/2008/06/04/1101970.html#1217512

StringOfChar 是反复 "字符" 成 "字符串" 的函数;

DupeString 是反复 "字符串" 成 "新字符串" 的函数;

StringOfChar 来自 System 单元, 可以直接使用;

DupeString 来自 StrUtils 单元, 使用时需要 uses StrUtils;

如果仅仅是反复 "字符", 当然应该用 StringOfChar , 它是用汇编代码实现的, 速度会稍好一些.

下面是测试代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses StrUtils; {DupeString 来自这个单元}

procedure TForm1.FormCreate(Sender: TObject);
var
  str: string;
begin
  str := System.StringOfChar('A', 5);
  ShowMessage(str); {AAAAA}

  str := StrUtils.DupeString('A', 5);
  ShowMessage(str); {AAAAA}

  str := StrUtils.DupeString('ABC', 5);
  ShowMessage(str); {ABCABCABCABCABC}
end;

{如果只是反复字符, StringOfChar 肯定会更快一些; 速度测试:}
procedure TForm1.Button1Click(Sender: TObject);
var
  t1,t2: Cardinal;
  i: Integer;
begin
  t1 := GetTickCount;
  for i := 0 to 1000000 do DupeString('A', 5);
  t1 := GetTickCount - t1;

  t2 := GetTickCount;
  for i := 0 to 1000000 do StringOfChar('A', 5);
  t2 := GetTickCount - t2;

  ShowMessageFmt('DupeString: %d; StringOfChar: %d', [t1,t2]);
end;

end.

posted on 2008-06-04 10:03  万一  阅读(...)  评论(...编辑  收藏