PrecisionTimer.pas 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. unit PrecisionTimer;
  2. interface
  3. uses
  4. Windows, Classes, SysUtils;
  5. type
  6. // Precision timer class declaration
  7. TPrecisionTimer = class
  8. OverheadTime: Int64;
  9. InitFreq: Int64;
  10. private
  11. { Private declarations }
  12. public
  13. { Public declarations }
  14. procedure Init;
  15. function GetCurrentTime(var lpPerformanceCount: Int64): Extended;
  16. function GetFrequency(): Int64;
  17. function GetPerformanceOverhead(): Int64;
  18. end;
  19. implementation
  20. // Initialize the timer
  21. procedure TPrecisionTimer.Init;
  22. var
  23. lpStart: Int64;
  24. lpEnd: Int64;
  25. begin
  26. // Calculate process overhead
  27. QueryPerformanceCounter(lpStart);
  28. QueryPerformanceCounter(lpEnd);
  29. OverheadTime := lpEnd - lpStart;
  30. // Get initial frequency
  31. QueryPerformanceFrequency(InitFreq);
  32. end;
  33. function TPrecisionTimer.GetCurrentTime(var lpPerformanceCount: Int64): Extended;
  34. begin
  35. QueryPerformanceCounter(lpPerformanceCount);
  36. lpPerformanceCount := lpPerformanceCount - GetPerformanceOverhead();
  37. end;
  38. function TPrecisionTimer.GetFrequency(): Int64;
  39. begin
  40. Result := InitFreq;
  41. end;
  42. function TPrecisionTimer.GetPerformanceOverhead(): Int64;
  43. begin
  44. Result := OverheadTime;
  45. end;
  46. end.