Originally Posted by
JSoftware
QueryPerformanceCounter :mrgreen:
Written for Delphi, but easy to adapt to FPC. My "Quick Timers" unit:
Code:
(******************************************************************************
* The contents of this file are subject to the Mozilla Public License *
* Version 1.1 (the "License"); you may not use this file except in *
* compliance with the License. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
* *
* Software distributed under the License is distributed on an "AS IS" *
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the *
* License for the specific language governing rights and limitations *
* under the License. *
* *
* The Original Code is uQTimers.pas. *
* *
* The Initial Developer of the Original Code is Robert "The Wicked Flea" *
* Kosek. Portions created by Robert Kosek are Copyright (C) May 2006. *
* All Rights Reserved. *
******************************************************************************)
unit uQTimers;
interface
uses Windows;
type
TQuickTimer = record
Start, Stop: Int64;
end;
{ Returns a number for the running timer. }
function StartTimer: Integer;
{ When given a number the timer will be removed and the result will be
the seconds elapsed. }
function StopTimer(which: Integer): Extended;
var ProcessorSpeed: Int64;
QTimers: array of TQuickTimer;
implementation
function StartTimer: Integer;
begin
Result := length(QTimers);
SetLength(QTimers,length(QTimers)+1);
QueryPerformanceCounter(QTimers[Result].Start);
end;
function StopTimer(which: Integer): Extended;
procedure RemoveIndex(Index: Integer);
var i: integer;
begin
for i := Index to Length(QTimers)-1 do
QTimers[i] := QTimers[i+1];
SetLength(QTimers,Length(QTimers)-1);
end;
begin
QueryPerformanceCounter(QTimers[which].Stop);
Result := (QTimers[which].Stop - QTimers[which].Start) / ProcessorSpeed;
if which = length(QTimers) then
SetLength(QTimers,length(QTimers)-1)
else
RemoveIndex(which);
end;
procedure KillTimers;
begin
SetLength(QTimers,0);
end;
initialization
QueryPerformanceFrequency(ProcessorSpeed);
finalization
KillTimers;
end.
Bookmarks