Difference between revisions of "QueryPerformanceCounter"

From Lazarus wiki
Jump to navigationJump to search
(Using Windows API Performance Counter)
 
m (Fixed syntax highlighting; removed categories included in template)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{QueryPerformanceCounter}}
 +
 
Maybe the one microsecond timestamp is too long for your program.
 
Maybe the one microsecond timestamp is too long for your program.
 
You can access the Windows API Performance Counter using code such as shown below.
 
You can access the Windows API Performance Counter using code such as shown below.
 
Note that this API may not be as reliable as you might hope.  For more information see http://www.virtualdub.org/blog/pivot/entry.php?id=106
 
Note that this API may not be as reliable as you might hope.  For more information see http://www.virtualdub.org/blog/pivot/entry.php?id=106
  
unit Unit1;  
+
<syntaxhighlight lang=pascal>
{$mode objfpc}{$H+}
+
unit Unit1;  
interface
+
{$mode objfpc}{$H+}
uses
+
interface
 +
 
 +
uses
 
   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
 
   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
 
   StdCtrls, Windows;
 
   StdCtrls, Windows;
type
+
 
 +
type
 
   { TForm1 }
 
   { TForm1 }
 
   TForm1 = class(TForm)
 
   TForm1 = class(TForm)
Line 20: Line 25:
 
  var
 
  var
 
   Form1: TForm1;  
 
   Form1: TForm1;  
implementation
+
implementation
{ TForm1 }
+
 
procedure TForm1.FormClick(Sender: TObject);
+
{ TForm1 }
var
+
 
 +
procedure TForm1.FormClick(Sender: TObject);
 +
var
 
   PerformanceCounter: int64;
 
   PerformanceCounter: int64;
 
   PerformanceFrequency: int64;
 
   PerformanceFrequency: int64;
begin
+
begin
 
   if QueryPerformanceFrequency(PerformanceFrequency)
 
   if QueryPerformanceFrequency(PerformanceFrequency)
    AND QueryPerformanceCounter(PerformanceCounter)
+
    and QueryPerformanceCounter(PerformanceCounter)
     then ShowMessage(
+
     then ShowMessage('Freq:' + IntToStr(PerformanceFrequency)
                    'Freq:'+IntToStr(PerformanceFrequency)
+
      + ', Counter:' + IntToStr(PerformanceCounter))
                    +', Counter:'+IntToStr(PerformanceCounter)
 
                    )
 
 
     else ShowMessage('Sorry, performance counters not supported.');
 
     else ShowMessage('Sorry, performance counters not supported.');
end;
+
end;
initialization
+
 
  {$I unit1.lrs}
+
initialization
end.
+
 
 +
{$I unit1.lrs}
 +
 
 +
end.
 +
</syntaxhighlight>

Latest revision as of 00:15, 24 February 2020

Windows logo - 2012.svg

This article applies to Windows only.

See also: Multiplatform Programming Guide

English (en)

Maybe the one microsecond timestamp is too long for your program. You can access the Windows API Performance Counter using code such as shown below. Note that this API may not be as reliable as you might hope. For more information see http://www.virtualdub.org/blog/pivot/entry.php?id=106

unit Unit1; 
{$mode objfpc}{$H+}
interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, Windows;

type
  { TForm1 }
  TForm1 = class(TForm)
    procedure FormClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 
 var
  Form1: TForm1; 
implementation

{ TForm1 }

procedure TForm1.FormClick(Sender: TObject);
var
  PerformanceCounter: int64;
  PerformanceFrequency: int64;
begin
  if QueryPerformanceFrequency(PerformanceFrequency)
    and QueryPerformanceCounter(PerformanceCounter)
    then ShowMessage('Freq:' + IntToStr(PerformanceFrequency)
       + ', Counter:' + IntToStr(PerformanceCounter))
    else ShowMessage('Sorry, performance counters not supported.');
end;

initialization

{$I unit1.lrs}

end.