Delphi-如何使用启动/停止功能以毫秒或纳秒为单位创建计时器?

Delphi-如何使用启动/停止功能以毫秒或纳秒为单位创建计时器?,delphi,timer,while-loop,milliseconds,Delphi,Timer,While Loop,Milliseconds,我在Delphi7中寻找以毫秒或纳秒为单位的计时器。我必须通过顺序搜索检查三个ISAM文件的速度。第一个ind文件包含50个字符串,如“record_0”到“record_50”。第二个是“记录0”到“记录500”,第三个是“记录0”到“记录5000”。我已经实现了一切,但我不知道如何制作计时器。我将字符串与每个ISAM文件中的最后一项进行比较。以下是我的第一个ind文件代码: procedure TForm1.Button1Click(Sender: TObject); var i:int

我在Delphi7中寻找以毫秒或纳秒为单位的计时器。我必须通过顺序搜索检查三个ISAM文件的速度。第一个ind文件包含50个字符串,如“record_0”到“record_50”。第二个是“记录0”到“记录500”,第三个是“记录0”到“记录5000”。我已经实现了一切,但我不知道如何制作计时器。我将字符串与每个ISAM文件中的最后一项进行比较。以下是我的第一个ind文件代码:

procedure TForm1.Button1Click(Sender: TObject);
  var i:integer;
  var content : String[20];
  var indexCounter:integer;
  var keyword:string;
begin
  //First ISAM file
  AssignFile(indF1, 'index1.ind');
  ReWrite(indF1);
  Reset(indF1);
  for i:=0 to 49 do begin
    content := 'record_';
    content := content + IntToStr(i+1);
    index1.index1 := content;
    index1.position1 := FileSize(indF1);
    Seek(indF1, FileSize(indF1));
    write(indF1, index1);
  end;
  CloseFile(indF1);
  Label12.Caption := FileSizeStr('index1.ind');

  //Sequential search in first ind file
  Reset(indF1);
  keyword := 'record_50';
  indexCounter := 0;
  //start timer
  while not Eof(indF1) do begin
    Seek(indF1, indexCounter);
    Read(indF1, Index1);
    if (keyword = Index1.index1) then begin
      //stop timer;
      //Label20 := milliseconds/nanoseconds;
      //return/break while loop (result := -1; exit;) ???
    end;
    indexCounter := indexCounter + 1;
  end;

我需要一个过程/函数,这样当我调用它时,它应该以毫秒或纳秒为单位开始计数,并在找到字符串时停止(它是每个ind文件中的最后一个字符串),并显示遍历所有文件所用的时间。我也不知道如何打破while循环。提前感谢。

使用绝地JCL。或者,如果您不想与绝地一起使用,请使用Win Api QueryPerformanceCounter。

此处描述的
TStopWatch
类具有所需的所有功能(适用于Delphi-7)

它在以后的Delphi版本(Delphi-2010)中作为单元诊断的高级记录实现

例如:

var
  sw : TStopWatch;
  elapsedMilliseconds : cardinal;
begin
  ...
  sw := TStopWatch.Create() ;
  try
    sw.Start;

    while not Eof(indF1) do begin
      Seek(indF1, indexCounter);
      Read(indF1, Index1);
      if (keyword = Index1.index1) then begin
        sw.Stop;
        Label20.Caption := IntToStr(sw.ElapsedMilliseconds);
        break; // break while loop
      end;
      indexCounter := indexCounter + 1;
    end;
    ...
  finally
    sw.Free;
  end;
end;
要中断while循环,只需执行
break在条件测试中。

使用和。第一个函数返回每秒的单位数,第二个函数返回一个值

lFreq: Int64;
InitialF, FinalF: Int64;

if QueryPerformanceFrequency(lFreq) then
  // hi-res timer is supported
else
  // hi-res timer is not supported

QueryPerformanceCounter(InitialF);
// do something you want to time
QueryPerformanceCounter(FinalF);
// duration of the time of something is FinalF - InitialF in "units"
// divide by lFreq to get the amount of time in seconds, 
// this will be an Extended type.

找到以下简单代码:

var
 StartTime : Cardinal;
begin
  StartTime := GetTickCount;
  //code to do
  ShowMessage(Format('Elapsed time %d ms', [GetTickCount - StartTime])); 

你为什么还在使用Pascal I/O?@iamjoosy,这个链接是一个可以在Delphi 7中工作的类,正如答案中所解释的。@LU RD oops,错过了这个链接。正如你所说,
TStopwatch
是一个高级记录-因此,它没有
.Free
方法,也不需要销毁。我不知道怎么做,你能用我的代码实现它吗?@Ezio_uu这里的信息都是你自己做的。StackOverflow不是“为我编写代码”服务,因此,请展示您的努力,如果您有更多问题,请返回此处,您将获得所需的帮助。此代码有一个问题-如果PC恰好运行了约49天(即2^32毫秒),则您的运行时间有可能为负,as
GetTickCount
包装为0。在Windows Vista或更高版本上,有一个
GetTickCount64
函数,它将使用一个64位无符号整数计算毫秒数,该整数应该足够5.85亿年使用,但我无法测试这个函数。