您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

91 行
1.9 KiB

  1. unit uutlTiming;
  2. { Package: Utils
  3. Prefix: utl - UTiLs
  4. Beschreibung: diese Unit enthält platformunabhängige Methoden für Zeitberechnungen und -messungen
  5. Mehr oder weniger platformunabhängige Timestampgenerierung.
  6. GetTickCount64 ms-Timer
  7. GetMicroTime us-Timer analog php microtime()
  8. Achtung: windows.pp deklariert auch eine GetTickCount64, wird gegen diese gelinkt
  9. läuft die exe nicht mehr unter XP. Unbeding uutlTiming *nach* windows einbinden. }
  10. {$mode objfpc}{$H+}
  11. interface
  12. uses
  13. Classes
  14. {$ifdef Windows}
  15. , Windows
  16. {$else}
  17. , Unix, BaseUnix
  18. {$endif};
  19. function GetTickCount64: QWord;
  20. function GetMicroTime: QWord;
  21. function utlRateLimited(const Reference: QWord; const Interval: QWord): boolean;
  22. implementation
  23. {$IF defined(WINDOWS)}
  24. var
  25. PERF_FREQ: Int64;
  26. function GetTickCount64: QWord;
  27. begin
  28. // GetTickCount64 is better, but we need to check the Windows version to use it
  29. Result := Windows.GetTickCount();
  30. end;
  31. function GetMicroTime: QWord;
  32. var
  33. pc: Int64;
  34. begin
  35. pc := 0;
  36. QueryPerformanceCounter(pc);
  37. Result:= (pc * 1000*1000) div PERF_FREQ;
  38. end;
  39. {$ELSEIF defined(UNIX)}
  40. function GetTickCount64: QWord;
  41. var
  42. tp: TTimeVal;
  43. begin
  44. fpgettimeofday(@tp, nil);
  45. Result := (Int64(tp.tv_sec) * 1000) + (tp.tv_usec div 1000);
  46. end;
  47. function GetMicroTime: QWord;
  48. var
  49. tp: TTimeVal;
  50. begin
  51. fpgettimeofday(@tp, nil);
  52. Result := (Int64(tp.tv_sec) * 1000*1000) + tp.tv_usec;
  53. end;
  54. {$ELSE}
  55. function GetTickCount64: QWord;
  56. begin
  57. Result := Trunc(Now * 24 * 60 * 60 * 1000);
  58. end;
  59. function GetMicroTime: QWord;
  60. begin
  61. Result := Trunc(Now * 24 * 60 * 60 * 1000*1000);
  62. end;
  63. {$ENDIF}
  64. function utlRateLimited(const Reference: QWord; const Interval: QWord): boolean;
  65. begin
  66. Result:= GetMicroTime - Reference > Interval;
  67. end;
  68. initialization
  69. {$IF defined(WINDOWS)}
  70. PERF_FREQ := 0;
  71. QueryPerformanceFrequency(PERF_FREQ);
  72. {$ENDIF}
  73. end.