我用循环生成数字字典,代码如下:
 for a:=0 to 9 do
  for b:=0 to 9 do
   for c:=0 to 9 do
    for d:=0 to 9 do
     for e:=0 to 9 do
      Richedit1.lines.add(inttostr(a)+inttostr(b)+inttostr(c)+inttostr(d)+inttostr(e));
然后显示在RichEdit控件中,5位数竟然用了6分钟,而我用小榕的流光自带的字典工具,6位数也不过几秒搞定。
请问高手,是不是一个一个显示在Richedit中变慢了呢,还是有好的算法呢,如果是用多线程,最好能举例,我对线程一窍不通。请指教,谢谢。

解决方案 »

  1.   

    var 
    linStr :string;
    begin
     for a:=0 to 9 do
      for b:=0 to 9 do
       for c:=0 to 9 do
        for d:=0 to 9 do
         for e:=0 to 9 do
           linStr:=linStr+inttostr(a)+inttostr(b)+inttostr(c)+inttostr(d)+inttostr(e)+ Chr(13) + Chr(10);
    Richedit1.text:=linstr;
    先暂时放到一个内存变量中,然后统一加载入richedit中
      

  2.   

    for i:=0 to 99999不就行了
      

  3.   

    for i:=0 to 99999 do
    begin
      richedit.lines.add(inttostr(i));
    end;
      

  4.   

    或者
    var
    s:string;
    begin
    for i:=0 to 99999 do
    begin
      s:=inttostr(i);
      while length(s)<5 do
      begin
        s:='0'+s;
      end;
      richedit.lines.add(s);
    end;
    end;
      

  5.   

    楼上的 "for i:=0 to 99999 do "根本不行,和我的代码生成速度是一样的。Arcan 的方法倒是快了,5位的只要几秒生成并显示出来,但5次运行只有1次能成功,其余4次程序失去了响应。6位的就更不用多说了,根本不能执行。
      

  6.   

    两种方法综合一下(for i:=0 to 99999 do 再放入一个变量),改善了许多,连6位的也可以顺利执行了,耗时11秒,C900,128m的机还算可以吧,但还是不能和流光的相比,因为7位的还不能执行(兰屏+死机),流光的7位最多30秒。
    不知道还有没有更好的方法?
      

  7.   

    在循环前面调用Richedit1.BeginUpdate();在循环后面加入Richedit1.EndUpdate()
      

  8.   

    你是要在richedit中显示所有的数字?7位的那可是1000万个啊,你的机器肯定是不行的。如果仅仅想把数字保存到文件中,那么我下面的代码生成1000万个(从0到9999999)只需要12秒钟(Athlon 900MHz 512MB内存),然后我在程序中打开这个高达86MB的文本文件花了3分40秒,在加载过程中耗用内存最高达380多MB,加载后程序占用内存237MB。所以你的机器就不要尝试我下面代码后半部分了。我的做法是直接往文件中写,这样速度很快,下面是我的全部代码:procedure TForm1.Button1Click(Sender: TObject);
    var
      TextFilevar:textfile;
      linStr :string;
      a:integer;
    begin
      AssignFile ( TextFileVar , 'c:\t.txt' );
      Rewrite(textfilevar);
      self.Caption :=timetostr(time());
      for a:=0 to 9999999 do
      begin
        linStr:=inttostr(a);
        WriteLn(textfilevar,linstr);
      end;
      self.Caption :=self.Caption +' '+timetostr(time());//完成到这就已经保存了1000万个数字,耗时12秒
      CloseFile(textfilevar);
      application.ProcessMessages ;
      richedit1.Lines.LoadFromFile('c:\t.txt');
      self.Caption :=self.Caption +' '+timetostr(time());//从保存完毕到加载完毕耗时3分40秒
    end;