3

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;

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.

Ezio_
  • 593
  • 3
  • 9
  • 23

4 Answers4

11

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

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

Example:

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;

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

LU RD
  • 34,438
  • 5
  • 88
  • 296
9

Use QueryPerformanceFrequency and QueryPerformanceCounter. The first function returns a number of units per second, and the second function returns a value.

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.
Glenn1234
  • 2,542
  • 1
  • 16
  • 21
  • I don't know how to do it, can you implement it with my code? – Ezio_ Feb 12 '13 at 15:47
  • 8
    @Ezio_ you have all the info here to do it by yourself. StackOverflow is not a _write the code for me_ service, so, show your effort and if you have more questions, come back here and you'll get the help you need. – jachguate Feb 12 '13 at 16:56
4

found this simple code:

var
 StartTime : Cardinal;
begin
  StartTime := GetTickCount;
  //code to do
  ShowMessage(Format('Elapsed time %d ms', [GetTickCount - StartTime])); 
Ezio_
  • 593
  • 3
  • 9
  • 23
  • One gotcha with this code- if the PC happens to have been running around 49 days (i.e. 2^32 milliseconds), your elapsed time runs the risk of being negative, as `GetTickCount` wraps to 0. On Windows Vista or later there's a `GetTickCount64` function that will count milliseconds using a 64-bit unsigned integer which should be sufficient for 585 million years, but I have been unable to test this. – Flynn1179 Aug 09 '17 at 13:07
1

Use JclCounter from Jedi JCL. Or if you don't want to go with Jedi, use Win Api QueryPerformanceCounter.

iamjoosy
  • 3,299
  • 20
  • 30