Re: [fpc-pascal] Sub-millisecond time measuring

2024-07-02 Thread Rafael Picanço via fpc-pascal
Hi everyone. I am sharing some cross-platform timing I have done, it would
be great to hear from you.

For mac I translated this python implementation to free pascal:
https://github.com/pupil-labs/pyuvc/blob/master/pyuvc-source/darwin_time.pxi
):
```
unit timestamps.machtime;

{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$linkframework CoreFoundation}

interface

type
  mach_timebase_info_data_t = record
numer: UInt32;
denom: UInt32;
  end;
  mach_timebase_info_t = ^mach_timebase_info_data_t;

function mach_absolute_time: UInt64; cdecl; external name
'mach_absolute_time';
function mach_timebase_info(info: mach_timebase_info_t): Integer; cdecl;
external name 'mach_timebase_info';

implementation

end.
```

However, I could not test it extensively. But here is some sample code:

```
uses ctypes,  timestamps.machtime;
{...}
var
  timeConvert: Float = 0.0;
//function get_sys_time_monotonic: Float;
function ClockMonotonic : Float;
var
  timeBase: mach_timebase_info_data_t;
begin
  if timeConvert = 0.0 then begin
mach_timebase_info(@timeBase);
timeConvert :=
  (Float(timeBase.numer) / Float(timeBase.denom) / Float(10.0);
  end;
  Result := mach_absolute_time() * timeConvert;
end;
```

For linux, I am used to clock_gettime (
https://linux.die.net/man/3/clock_gettime):
```
unit timestamps.gnulinux;
uses Linux, UnixType, Math;
{...}
function ClockMonotonic: Float;
var
  tp: timespec;
  a, b : Float;
begin
  clock_gettime(CLOCK_MONOTONIC, @tp);
  a := Float(tp.tv_sec);
  b := Float(tp.tv_nsec) * 1e-9;
  Result := a+b;
end;
```
_
For windows I am used to QueryPerformanceCounter:
```
unit timestamps.windows;
uses Windows, Math;
{...}
var
  PerSecond : TLargeInteger;
function ClockMonotonic: Float;
var
  Count : TLargeInteger;
begin
  QueryPerformanceCounter(Count);
  Result := Float(Count) / Float(PerSecond);
end;

initialization
   QueryPerformanceFrequency(PerSecond);
```
Of course, you don't need any "ToSeconds" conversion if you don't want to.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-07-01 Thread Adriaan van Os via fpc-pascal

Sven Barth via fpc-pascal wrote:

I wouldn't introduce a dependency for something like GTK (especially 
GTK!) on macOS only to receive some timings. Simpler and more straight 
forward to just use the correct functions of the OS. 


Of course not, Unless you are doing some cross-development, developing on MacOS for gtk and larer 
moving it native to Linux. Then, you will run into the bug.


Regards,

Adriaan van Os

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-07-01 Thread Michael Van Canneyt via fpc-pascal




On Mon, 1 Jul 2024, Sven Barth via fpc-pascal wrote:


Adriaan van Os via fpc-pascal  schrieb am
Sa., 29. Juni 2024, 21:21:


Michael Van Canneyt via fpc-pascal wrote:

Is that a function in the RTL? I can't find it.


No, these are platform-specific functions.

g_get_monotonic_time is probably just an alias for the linux/freebsd
unit functions clock_gettime(CLOCK_MONOTONIC_RAW).


g_get_monotonic_time is in glib <
https://docs.gtk.org/glib/func.get_monotonic_time.html>

  function g_get_monotonic_time: gint64; cdecl; external;

but it requires a bug fix when using gtk on MacOSX <
https://bugzilla.gnome.org/show_bug.cgi?id=728123>



I wouldn't introduce a dependency for something like GTK (especially GTK!)
on macOS only to receive some timings. Simpler and more straight forward to
just use the correct functions of the OS.


Exactly. 
That is why I gave the origins of these functions, instead of
high-level wrappers like g_get_monotonic_time or even epiktimer 
(a monstrosity in itself).


Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-07-01 Thread Sven Barth via fpc-pascal
Adriaan van Os via fpc-pascal  schrieb am
Sa., 29. Juni 2024, 21:21:

> Michael Van Canneyt via fpc-pascal wrote:
> >> Is that a function in the RTL? I can't find it.
> >
> > No, these are platform-specific functions.
> >
> > g_get_monotonic_time is probably just an alias for the linux/freebsd
> > unit functions clock_gettime(CLOCK_MONOTONIC_RAW).
>
> g_get_monotonic_time is in glib <
> https://docs.gtk.org/glib/func.get_monotonic_time.html>
>
>   function g_get_monotonic_time: gint64; cdecl; external;
>
> but it requires a bug fix when using gtk on MacOSX <
> https://bugzilla.gnome.org/show_bug.cgi?id=728123>
>

I wouldn't introduce a dependency for something like GTK (especially GTK!)
on macOS only to receive some timings. Simpler and more straight forward to
just use the correct functions of the OS.

Regards,
Sven

>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-29 Thread Adriaan van Os via fpc-pascal

Michael Van Canneyt via fpc-pascal wrote:

Is that a function in the RTL? I can't find it.


No, these are platform-specific functions.

g_get_monotonic_time is probably just an alias for the linux/freebsd 
unit functions clock_gettime(CLOCK_MONOTONIC_RAW).


g_get_monotonic_time is in glib 


 function g_get_monotonic_time: gint64; cdecl; external;

but it requires a bug fix when using gtk on MacOSX 


Regards,

Adriaan van Os

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-29 Thread Graeme Geldenhuys via fpc-pascal
On 28/06/2024 14:31, Hairy Pixels via fpc-pascal wrote:
> Thanks I'll check that out. I'm not using Lazarus so tons of dependencies 
> won't work

It's a Lazarus package (for convenience), but it doesn't depend on the
LCL. You can use it with pure FPC programs, or even without the Lazarus
package file.

Regards,
  Graeme

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-29 Thread Graeme Geldenhuys via fpc-pascal
On 28/06/2024 22:50, Hairy Pixels via fpc-pascal wrote:
> But that's still millisecond precision.

On Windows, Linux and FreeBSD it uses nanosecond precision.
Unfortunately on MacOS, it falls back to RTL's GetTickCount64(), which
as you mentioned, is millisecond precision. It shouldn't be hard to add
higher precision for MacOS too - you just need the correct system call.
EpikTimer's code is pretty simple to follow, if you wanted to take a look.

Regards,
  Graeme

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-29 Thread Michael Van Canneyt via fpc-pascal



On Sat, 29 Jun 2024, Hairy Pixels via fpc-pascal wrote:





On Jun 29, 2024, at 5:43 PM, Adriaan van Os  wrote:

Hairy Pixels via fpc-pascal wrote:

I had a large function which I was profiling with MilliSecondsBetween but I 
split it to be called many different times and now it's not accumulating the 
total time correctly because time is being lost to due millisecond precision.
Is there anything in the RTL I can use which is more accurate? I searched and 
couldn't find anything.


You can use Microseconds on MacOS, QueryPerformanceFrequency on Windows and 
g_get_monotonic_time with gtk on Linux.

Regards,

Adriaan van Os



Is that a function in the RTL? I can't find it.


No, these are platform-specific functions.

g_get_monotonic_time is probably just an alias for the linux/freebsd unit functions 
clock_gettime(CLOCK_MONOTONIC_RAW).


QueryPerformanceFrequency you will probably find in the Windows unit.

Microseconds can be found in the Timer unit (part of univint package)

Michael.___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-29 Thread Hairy Pixels via fpc-pascal


> On Jun 29, 2024, at 5:43 PM, Adriaan van Os  wrote:
> 
> Hairy Pixels via fpc-pascal wrote:
>> I had a large function which I was profiling with MilliSecondsBetween but I 
>> split it to be called many different times and now it's not accumulating the 
>> total time correctly because time is being lost to due millisecond precision.
>> Is there anything in the RTL I can use which is more accurate? I searched 
>> and couldn't find anything.
> 
> You can use Microseconds on MacOS, QueryPerformanceFrequency on Windows and 
> g_get_monotonic_time with gtk on Linux.
> 
> Regards,
> 
> Adriaan van Os
> 

Is that a function in the RTL? I can't find it.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-29 Thread Adriaan van Os via fpc-pascal

Hairy Pixels via fpc-pascal wrote:

I had a large function which I was profiling with MilliSecondsBetween but I 
split it to be called many different times and now it's not accumulating the 
total time correctly because time is being lost to due millisecond precision.

Is there anything in the RTL I can use which is more accurate? I searched and 
couldn't find anything.


You can use Microseconds on MacOS, QueryPerformanceFrequency on Windows and g_get_monotonic_time 
with gtk on Linux.


Regards,

Adriaan van Os

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-28 Thread Hairy Pixels via fpc-pascal


> On Jun 28, 2024, at 10:44 PM, Christo Crause  wrote:
> 
> It is based on TComponent from the RTL classes unit, so no dependency on LCL 
> or other Lazarus units.  A quick check of the source code suggests that it 
> will probably call GetTickCount64 
> (https://github.com/graemeg/epiktimer/blob/master/epiktimer.pas#L419C15-L419C29)
>  on macOS.


But that's still millisecond precision. Maybe this an OS problem more than 
anything. I'll check to see what C libraries exist on my system as I may need 
to go that route. I don't see anything in the FPC RTL that's sub-millisecond 
and there may be a reason for that.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-28 Thread Christo Crause via fpc-pascal
On Fri, Jun 28, 2024 at 5:31 PM Hairy Pixels via fpc-pascal <
fpc-pascal@lists.freepascal.org> wrote:

> Thanks I'll check that out. I'm not using Lazarus so tons of dependencies
> won't work but maybe I can learn something. I'm on macOS too so if this is
> using system calls it may not be supported.
>

It is based on TComponent from the RTL classes unit, so no dependency on
LCL or other Lazarus units.  A quick check of the source code suggests that
it will probably call GetTickCount64 (
https://github.com/graemeg/epiktimer/blob/master/epiktimer.pas#L419C15-L419C29)
on macOS.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-28 Thread Hairy Pixels via fpc-pascal
Thanks I'll check that out. I'm not using Lazarus so tons of dependencies won't 
work but maybe I can learn something. I'm on macOS too so if this is using 
system calls it may not be supported.


> On Jun 28, 2024, at 10:10 PM, Christo Crause  wrote:
> 
> 
> On Fri, Jun 28, 2024 at 2:34 PM Hairy Pixels via fpc-pascal 
>  wrote:
> I had a large function which I was profiling with MilliSecondsBetween but I 
> split it to be called many different times and now it's not accumulating the 
> total time correctly because time is being lost to due millisecond precision.
> 
> Is there anything in the RTL I can use which is more accurate? I searched and 
> couldn't find anything.
> 
> In the past I've used EpikTimer (https://wiki.freepascal.org/EpikTimer), it 
> is cross platform and works well, at least for my casual use.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sub-millisecond time measuring

2024-06-28 Thread Christo Crause via fpc-pascal
On Fri, Jun 28, 2024 at 2:34 PM Hairy Pixels via fpc-pascal <
fpc-pascal@lists.freepascal.org> wrote:

> I had a large function which I was profiling with MilliSecondsBetween but
> I split it to be called many different times and now it's not accumulating
> the total time correctly because time is being lost to due millisecond
> precision.
>
> Is there anything in the RTL I can use which is more accurate? I searched
> and couldn't find anything.
>

In the past I've used EpikTimer (https://wiki.freepascal.org/EpikTimer), it
is cross platform and works well, at least for my casual use.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Sub-millisecond time measuring

2024-06-28 Thread Hairy Pixels via fpc-pascal
I had a large function which I was profiling with MilliSecondsBetween but I 
split it to be called many different times and now it's not accumulating the 
total time correctly because time is being lost to due millisecond precision.

Is there anything in the RTL I can use which is more accurate? I searched and 
couldn't find anything.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal