Почему время QueryperformanceCounter отличается от времени настенных часов?

Привет, я использую QueryperformanceCounter для измерения времени блока кода в Delphi. По какой-то причине Число в миллисекундах, которое я получил с помощью QueryPerformanceCounter, сильно отличается от времени моих настенных часов с использованием секундомера. Например, секундомер дает мне около 33 секунд, что кажется правильным, если не точностью, но использование QueryPerofomanceCounter даст мне число, например, 500 миллисекунд.

Когда пройдемся по моему коду, я вижу, что QueryPerformanceFrequency дает мне правильную частоту процессора для моего процессора , 2.4G для Core2 E6600. Итак, если номер тика правильный, (номер тика / частота) * 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.
5
задан Paul L 21 June 2011 в 05:08
поделиться