Пример реализации высокоточного таймера на Pascal.
Был сделан в целях замера производительности алгоритмов сортировки с
использованием QueryPerformanceCounter и QueryPerformanceFrequency.
Написано на Pascal.
function getTimeElapsed (t1: int64; t2: int64; freq: int64): string;
const
milli : extended = 1E-3;
micro : extended = 1E-6;
nano : extended = 1E-9;
pico : extended = 1E-12;
begin
if (freq * pico) >= 1 then
begin
Result := FloatToStr( (t2-t1) / (freq*pico) ) + ' пикосек.';
exit;
end;
if (freq * nano) >= 1 then
begin
Result := FloatToStr( (t2-t1) / (freq*nano) ) + ' наносек.';
exit;
end;
if (freq * micro) >= 1 then
begin
Result := FloatToStr( (t2-t1) / (freq*micro) ) + ' микросек.';
exit;
end;
if (freq * milli) >= 1 then
begin
Result := FloatToStr( (t2-t1) / (freq*milli) ) + ' миллисек.';
exit;
end;
end;
Далее делаем вызов, должно быть нечто вроде:
var
timer1, timer2, timerfreq: int64;
result: string;
begin
QueryPerformanceFrequency(timerfreq);
QueryPerformanceCounter(timer1); // выполняем наш код
QueryPerformanceCounter(timer2);
result := getTimeElapsed(timer1, timer2, timerfreq);
end;