[fpc-pascal] Microsecond Delay Suggestions?

Dmitry Boyarintsev skalogryz.lists at gmail.com
Tue Jul 26 17:44:44 CEST 2016


On Tue, Jul 26, 2016 at 11:21 AM, Dmitry Boyarintsev <
skalogryz.lists at gmail.com> wrote:

> Maybe you want to look into QueryPerformanceCounter.
>
> https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx
>
> Here's an example:

{$mode objfpc}{$H+}

uses
  Windows;

const
  MSInSec = 1000;
  MCInSec = MSInSec * 1000;
  NSInSec = MCInSec * 1000;

function LargeLoop: Integer;
var
  i : integer;
begin
  Result:=0;
  for i:=0 to 1000 do
    inc(Result);
   //or you can verify Sleep(milliseconds)
   //Sleep(100);
end;

var
  fr : TLargeInteger;
  ct : TLargeInteger;
  af : TLargeInteger;
  d  : double;
  diff : TLargeInteger;
begin
  QueryPerformanceFrequency(fr);
  writeln('Frequency: ' ,fr);
  d:=(1/fr) * NSInSec;
  writeln('Freq in Time: ', d:0:0, ' ns (roughly)');
  QueryPerformanceCounter(ct);
  LargeLoop;
  QueryPerformanceCounter(af);
  diff := af - ct;
  writeln('Loop:');
  writeln('  Ticks: ', diff);
  writeln('  Time:  ', ((diff/fr) * NSInSec):0:0,' ns' );
  writeln('         ', ((diff/fr) * MCInSec):0:6,' mcs' );
  writeln('         ', ((diff/fr) * MSInSec):0:6,' ms' );
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160726/e602907d/attachment.html>


More information about the fpc-pascal mailing list