Delphi - 如何使用启动/停止功能以毫秒或纳秒为单位制作计时器? [英] Delphi - How to make timer in milliseconds or nanoseconds with start/stop functions?

查看:55
本文介绍了Delphi - 如何使用启动/停止功能以毫秒或纳秒为单位制作计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a timer in milliseconds or nanoseconds in Delphi7. I have to check the speeds of three ISAM files with sequential search. The first ind file contains 50 strings like "record_0" to "record_50". The second - "record_0" to "record_500" and the third - "record_0" to "record_5000". I've implemented everything but I don't know how to make the timer. I am comparing a string with the last item in each ISAM file. Here is my code for the first ind file:

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 循环.提前致谢.

I need a procedure/function so that when I call it it should start counting in milliseconds or nanoseconds and stop when the string is found (it's the last string in each ind file) and show the elapsed time for traversing through all the file. Also I don't know how to break the while loop. Thanks in advance.

推荐答案

此处描述的 TStopWatch"delphi-high-performance-timer-tstopwatch" 拥有所有需要的功能(对于 Delphi-7).

The TStopWatch class described here "delphi-high-performance-timer-tstopwatch" has all functions needed (for Delphi-7).

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

It's implemented in later Delphi versions (Delphi-2010) as an advanced record in unit diagnostics.

示例:

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; 即可.

To break the while loop, just do break; inside your conditional test.

这篇关于Delphi - 如何使用启动/停止功能以毫秒或纳秒为单位制作计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆