SkillAgentSearch skills...

HiResStopWatch

High resolution stopwatch using RDTSCP.

Install / Use

/learn @JBontes/HiResStopWatch
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

HiResStopWatch

High resolution stopwatch using RDTSCP.

Easy to use

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:

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.000.000 is normal for single cycle timings.
100.000 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.

View on GitHub
GitHub Stars9
CategoryDevelopment
Updated3y ago
Forks4

Languages

Pascal

Security Score

55/100

Audited on Jul 16, 2022

No findings