数据读取代码
  type
    TMethod = Record
    Time:   TDateTime;
    Value:  array[1..4] of Single;
    Status: array[1..4] of byte;
    end;
var
  MyRec:         Tmethod;
procedure TForm1.ColorButton1Click(Sender: TObject);
var s:             string;
    TemFileName:   string;
    TemNum:        Integer;
    FiletoRead:    file of Tmethod;
    empty:         array[1..4] of string;
    i:byte;
begin
    i:=0;
    Memo1.Lines.Clear;
    OpenDialog1.Execute;
    s:=OpenDialog1.FileName;
    TemFileName:='data'+'.dat';    TemNum:=Pos(TemFileName,s);
    if TemNum=0 then  exit;
    AssignFile(FiletoRead,OpenDialog1.FileName);
    Reset(Filetoread);
    While not Eof(Filetoread) do
    begin
      Read(FiletoRead,Myrec);
      for i:=1 to 4 do
      begin
        if trunc(Myrec.Value[i])<10   then Empty[i]:='    ' else
        if trunc(Myrec.Value[i])<100  then Empty[i]:='   '  else
        if trunc(Myrec.Value[i])<1000 then Empty[i]:='  '   else
        Empty[i]:=' ';
      end;
      Memo1.Lines.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss:zzz',Myrec.Time)+empty[1]+FormatFloat('0.00',Myrec.value[1])+empty[2]+FormatFloat('0.00',Myrec.value[2])+empty[3]+FormatFloat('0.00',Myrec.value[3])+empty[4]+FormatFloat('0.00',Myrec.value[4])
                           +' '+intTostr(Myrec.Status[1])+' '+intTostr(Myrec.Status[2])+' '+intTostr(Myrec.Status[3])+' '+intTostr(Myrec.Status[4]));
    end;
end;
数据存储代码:
var 
          Filetosave: file of Tmethod;            Seek(Filetosave,FileSize(Filetosave)); //打开文件并将指针移到文件结尾
            Try
                Write(Filetosave,MyRec);  //在文件结尾写入纪录格式的数据值
以上是我编的一段读取二进制的小程序,当数据少的时候,读取速度还可以,但当数据量很大时,例如7M,读取的速度非常慢,大家帮忙看下是什么原因,如何解决,谢谢

解决方案 »

  1.   

    我感觉你写的没有问题,速度慢可能是写memo造成的,你可以把写入memo的代码注释掉,测试一下读的速度。看有问题没?
      

  2.   

    确实是像您所说的,把memo代码注释掉后,非常快,但是我要把数据显示出来,该用哪个插件呢?
      

  3.   


      Memo1.Lines.BeginUpdate;
      //for循环加入数据
      ......
      Memo1.Lines.EndUpdate;
    这样可能会速度快一些,试试
      

  4.   

    写错了,在While循环前后加上
      

  5.   

    你可以用Tfilestream读取文件,用MEMO1.Lines.LoadFromStream();
      

  6.   

    你可以用Tfilestream读取文件,用MEMO1.Lines.LoadFromStream();
    用文件流可能比BeginUpdate;
    还要快点
      

  7.   

    是这样用么?
        if OpenDialog1.Execute then
           begin
           F:=Tfilestream.Create(OpenDialog1.FileName,Fmopenread);
           Memo1.Lines.LoadFromStream(F);
           end;
    好像不对啊,帮忙写下吧,谢谢了
      

  8.   

    我测试过了,你看速度怎样//写数据
    procedure TForm1.Button1Click(Sender: TObject);
    var
      fs: Tfilestream;
      i  :integer;
      myrec: TMethod;
    begin
      myrec.Time:=now;
      for i:=1 to  4 do
      begin
        myrec.Value[i]:=i/100;
        myrec.Status[i]:=i;
      end;
      fs:=Tfilestream.Create('d:\1.ttt',fmCreate);
      fs.Position:=0;
      for i:=0 to 0 do   //i 的数量自己定
        fs.Write(myrec,sizeof(myrec));
      fs.Free;
    end;
    //读数据
    procedure TForm1.Button2Click(Sender: TObject);var
      fs: Tfilestream;
      i  :integer;
      myrec: TMethod;
      st:  Tstringlist;
      empty:        array[1..4] of string;
    begin
      st:=Tstringlist.Create;
      fs:=Tfilestream.Create('d:\1.ttt',fmOpenRead);
      fs.Position:=0;
      for i:=0 to (fs.Size div sizeof(myrec))-1 do
      begin
        fs.read(myrec,sizeof(myrec));
        st.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss:zzz',Myrec.Time)+empty[1]+FormatFloat('0.00',Myrec.value[1])+empty[2]+FormatFloat('0.00',Myrec.value[2])+empty[3]+FormatFloat('0.00',Myrec.value[3])+empty[4]+FormatFloat('0.00',Myrec.value[4])
                              +' '+intTostr(Myrec.Status[1])+' '+intTostr(Myrec.Status[2])+' '+intTostr(Myrec.Status[3])+' '+intTostr(Myrec.Status[4]));
      end;
      memo1.Lines:=st;
      fs.Free;
      st.Free;
    end;
      

  9.   

    改进了一下,大概20sprocedure TForm1.Button2Click(Sender: TObject);
    var
      fs: Tfilestream;
      i  :integer;
      myrec: TMethod;
      st:  Tstringlist;
      empty:        array[1..4] of string;
    begin
      st:=Tstringlist.Create;
      fs:=Tfilestream.Create('d:\1.ttt',fmOpenRead);
      fs.Position:=0;
      for i:=0 to (fs.Size div sizeof(myrec))-1 do
      begin
        fs.read(myrec,sizeof(myrec));
        st.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss:zzz',Myrec.Time)+empty[1]+FormatFloat('0.00',Myrec.value[1])+empty[2]+FormatFloat('0.00',Myrec.value[2])+empty[3]+FormatFloat('0.00',Myrec.value[3])+empty[4]+FormatFloat('0.00',Myrec.value[4])
                              +' '+intTostr(Myrec.Status[1])+' '+intTostr(Myrec.Status[2])+' '+intTostr(Myrec.Status[3])+' '+intTostr(Myrec.Status[4]));
      end;
      st.SaveToFile('d:\1.txt');
      memo1.Lines.LoadFromFile('d:\1.txt');
      fs.Free;
      st.Free;end;