Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

HiResStopWatch

High resolution stopwatch using RDTSCP.

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

HiResStopWatch

High resolution stopwatch using RDTSCP.

Easy to use

delphi
procedure TForm1.Button5Click(Sender: TObject);
const
  repeats = 1000000;
var
  Timer: THiResStopWatch;
  Time1, Time2: int64;
begin
  Time1:= Timer.Sample(TimeInRange, repeats, EmptyProc);
  Time2:= Timer.Sample(TimeCase, repeats, EmptyProc);
  Memo1.Lines.Add(Format('InRange takes %d cycles',[Time1]));
  Memo1.Lines.Add(Format('case takes %d cycles',[Time2]));
end;

TimeInRange, TimeCase and EmptyProc are simple procedures that look like:

delphi
procedure TForm1.EmptyProc;
begin
end;

Accurate

Given enough repeats the timer is accurate to within 2 CPU cycles.
The timer knows about out-of-order execution and eliminates this issue as per Intel recommendations. The shorter the timing, the more repeats are needed.

  1. 00 is normal for single cycle timings.
  2. 00 for 100 cycle timings.
    and 10.000 for 1000 cycle timings
    etc.

If your code takes longer you might as well use the normal TStopWatch.