Привет! Я использую QueryperformanceCounter для определения времени блока кода в Delphi. По какой-то причине
Миллисекундный номер, который я получил с помощью QueryPerformanceCounter, довольно сильно отличается от моего времени на настенных часах с помощью секундомера. Например, секундомер дает мне около 33 секунд, что кажется правильным, если не точностью, но использование QueryPerofomanceCounter даст мне число, например, 500 миллисекунд.
При выполнении моего кода я вижу, что QueryPerformanceFrequency дает мне правильную частоту ЦП для моего ЦП, 2,4 Г для Core2 E6600. Так что, если номер галочки правильный, (tick number / Freq) * 1000
должен дать мне правильное время выполнения для кода, который я синхронизирую, но почему бы и нет?
Я знаю, что для кода, который я пытаюсь синхронизировать, QeuryPerformanceCounter, вероятно, чрезмерно убивает, поскольку это заняло секунды, а не миллионы секунд, но мне больше интересно понять причину разницы во времени между настенными часами и QueryPerormanceCounter. *
Мое оборудование - E6600 Core2, а ОС - Windows 7 X64, если это необходимо.
unit PerformanceTimer;
interface
uses Windows, SysUtils, DateUtils;
type TPerformanceTimer = class
private
fFrequency : TLargeInteger;
fIsRunning: boolean;
fIsHighResolution: boolean;
fStartCount, FstopCount : TLargeInteger;
procedure SetTickStamp(var lInt : TLargeInteger) ;
function GetElapsedTicks: TLargeInteger;
function GetElapsedMiliseconds: TLargeInteger;
public
constructor Create(const startOnCreate : boolean = false) ;
procedure Start;
procedure Stop;
property IsHighResolution : boolean read fIsHighResolution;
property ElapsedTicks : TLargeInteger read GetElapsedTicks;
property ElapsedMiliseconds : TLargeInteger read GetElapsedMiliseconds;
property IsRunning : boolean read fIsRunning;
end;
implementation
constructor TPerformanceTimer.Create(const startOnCreate : boolean = false) ;
begin
inherited Create;
fIsRunning := false;
fIsHighResolution := QueryPerformanceFrequency(fFrequency) ;
if NOT fIsHighResolution then
fFrequency := MSecsPerSec;
if startOnCreate then
Start;
end;
function TPerformanceTimer.GetElapsedTicks: TLargeInteger;
begin
result := fStopCount - fStartCount;
end;
procedure TPerformanceTimer.SetTickStamp(var lInt : TLargeInteger) ;
begin
if fIsHighResolution then
QueryPerformanceCounter(lInt)
else
lInt := MilliSecondOf(Now) ;
end;
function TPerformanceTimer.GetElapsedMiliseconds: TLargeInteger;
begin
result := (MSecsPerSec * (fStopCount - fStartCount)) div fFrequency;
end;
procedure TPerformanceTimer.Start;
begin
SetTickStamp(fStartCount) ;
fIsRunning := true;
end;
procedure TPerformanceTimer.Stop;
begin
SetTickStamp(fStopCount) ;
fIsRunning := false;
end;
end.