[fpc-pascal] Microsecond Delay Suggestions?

James Richters james at productionautomation.net
Tue Jul 26 18:43:48 CEST 2016


That looks great.   I’ll give it a try!  Thank you very much for the suggestion and the example

 

From: fpc-pascal-bounces at lists.freepascal.org [mailto:fpc-pascal-bounces at lists.freepascal.org] On Behalf Of Dmitry Boyarintsev
Sent: Tuesday, July 26, 2016 11:45 AM
To: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
Subject: Re: [fpc-pascal] Microsecond Delay Suggestions?

 

On Tue, Jul 26, 2016 at 11:21 AM, Dmitry Boyarintsev <skalogryz.lists at gmail.com <mailto: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 <https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904%28v=vs.85%29.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/f64627f4/attachment.html>


More information about the fpc-pascal mailing list