On Thursday 03 of November 2011 08:43:55 Martin Schreiber wrote:
> On Thursday 03 November 2011 08.04:17 Martin Schreiber wrote:
> > On Thursday 03 November 2011 07.44:47 zeljko wrote:
> > > > The results with 10'000'000 calls:
> > > > 
> > > > FPC Now() MSEgui nowutc() MSEgui nowlocal()
> > > > 
> > > > Linux
> > > > 
> > > >   15.29s           3.39s             3.57s
> > > > 
> > > > Windows
> > > > 
> > > >   10.00s           1.22s             1.37s
> > > 
> > > Have you tried latest Michael's patch for getttimeofday ?
> 
> RTL recompiled with Michael's file:
> 
>  Linux
>  FPC Now() MSEgui nowutc() MSEgui nowlocal()
> 
>   11.49s           3.86s             4.03s

That's pretty big difference. Can you compare NowReal() from attached program 
with your functions ?

zeljko


program unixclocks;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils,
  unix, baseunix, libc, syscall, unixutil;

const
  CLOCK_REALTIME = 0;
  CLOCK_MONOTONIC = 1;
  CLOCK_MONOTONIC_RAW = 4;
  CLOCK_REALTIME_COARSE = 5;
  CLOCK_MONOTONIC_COARSE = 6;
  CLOCKS_MASK = CLOCK_REALTIME or CLOCK_MONOTONIC;

procedure GetLocalTimeNew(var SystemTime: TSystemTime; const AClockType: byte);
var
  ASpec: TimeSpec;
begin
  if Do_SysCall(syscall_nr_clock_gettime, AClockType, TSysParam(@ASpec)) = 0 then
  begin
    with SystemTime do
    begin
      EpochToLocal(ASpec.tv_sec, year, month, day, hour, minute, second);
      Millisecond := ASpec.tv_nsec mod 1000;
    end;
  end;
end;

function NowReal: TDateTime;
var
  SystemTime: TSystemTime;
begin
  GetLocalTimeNew(SystemTime, CLOCK_REALTIME);
  Result := SystemTimeToDateTime(SystemTime);
end;

function NowMono: TDateTime;
var
  SystemTime: TSystemTime;
begin
  GetLocalTimeNew(SystemTime, CLOCK_MONOTONIC);
  Result := SystemTimeToDateTime(SystemTime);
end;

function NowLibc: TDateTime;
var
  T: Libc.time_t;
  TV: Libc.TimeVal;
  UT: Libc.TUnixTime;
begin

  Libc.gettimeofday(TV, nil);
  T := TV.tv_sec;
  Libc.localtime_r(@T, @UT);
  Result := EncodeDate(UT.tm_year + 1900, UT.tm_mon + 1, UT.tm_mday) +
    EncodeTime(UT.tm_hour, UT.tm_min, UT.tm_sec, TV.tv_usec div 1000);
end;

function GetTickCount(const AType: Integer = 0): DWord;
var
  ATime: TDateTime;
begin

  case AType of
    1: ATime := NowReal();
    2: ATime := NowLibc();
    else
      ATime := Now();
  end;
  Result := DWord(Trunc(ATime * 24 * 60 * 60 * 1000))
end;

const
  AMsrInt = 10000000;
var
  i: integer;
  ATicks: DWord;


begin
  writeln('RTL ',FormatDateTime('dd.mm.yyyy hh:nn:ss.zz ',Now()),' clock_gettime() ',
  FormatDateTime('dd.mm.yyyy hh:nn:ss.zz ',NowReal()),
  ' libc ',FormatDateTime('dd.mm.yyyy hh:nn:ss.zz ',NowLibc()));
  ATicks := GetTickCount;
  for i := 0 to AMsrInt - 1 do
    Now();
  writeln(Format('RTL Now() with %d calls = %d ms ', [AMsrInt,
    GetTickCount - ATicks]));

  ATicks := GetTickCount;
  for i := 0 to AMsrInt - 1 do
    NowReal();
  writeln(Format('Kernel clock_gettime() NowReal() with %d calls = %d ms ',
    [AMsrInt, GetTickCount - ATicks]));

  ATicks := GetTickCount;
  for i := 0 to AMsrInt - 1 do
    NowLibc();
  writeln(Format('Libc gettimeofday()+localtime_r() with %d calls = %d ms ',
    [AMsrInt, GetTickCount - ATicks]));

end.

_______________________________________________
fpc-devel maillist  -  [email protected]
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to