【问题标题】:Delphi - How to make timer in milliseconds or nanoseconds with start/stop functions?Delphi - 如何使用启动/停止功能以毫秒或纳秒为单位制作计时器?
【发布时间】:2013-02-12 14:13:29
【问题描述】:

我在 Delphi7 中寻找以毫秒或纳秒为单位的计时器。我必须通过顺序搜索检查三个 ISAM 文件的速度。第一个 ind 文件包含 50 个字符串,例如“record_0”到“record_50”。第二个 - “record_0”到“record_500”,第三个 - “record_0”到“record_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循环。提前致谢。

【问题讨论】:

  • 你为什么还在使用 Pascal I/O?

标签: delphi timer while-loop milliseconds


【解决方案1】:

这里描述的TStopWatch"delphi-high-performance-timer-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;

【讨论】:

  • @iamjoosy,该链接是一个可以在 Delphi 7 中使用的类,如答案中所述。
  • @LU RD 哎呀,错过了链接。
  • 如你所说,TStopwatch 是高级记录——因此,它没有.Free 方法,也不需要销毁。
【解决方案2】:

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

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.

【讨论】:

  • 不知道怎么做,能不能用我的代码实现一下?
  • @Ezio_ 你在这里有所有信息可以自己做。 StackOverflow 不是为我编写代码服务,因此,请展示你的努力,如果你有更多问题,请回到这里,你会得到你需要的帮助。
【解决方案3】:

找到了这个简单的代码:

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

【讨论】:

  • 使用此代码的一个问题 - 如果 PC 恰好运行了大约 49 天(即 2^32 毫秒),那么您经过的时间就有可能是负数,因为 GetTickCount 换行为 0 . 在 Windows Vista 或更高版本上,有一个 GetTickCount64 函数将使用 64 位无符号整数计算毫秒数,这应该足够 5.85 亿年,但我无法对此进行测试。
【解决方案4】:

使用 Jedi JCL 的 JclCounter。或者,如果您不想使用 Jedi,请使用 Win Api QueryPerformanceCounter。

【讨论】:

    猜你喜欢
    • 2023-02-07
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2017-07-10
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多