Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2024-06

Sub-millisecond time measuring

14 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hairy Pixels

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

Christo Crause
to Hairy Pixels

On Fri, Jun 28, 2024 at 2:34 PM Hairy Pixels via fpc-pascal <

Show quoted text 10 lines
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.

Hairy Pixels
to Christo Crause

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.

Show quoted text 9 lines
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

Christo Crause
to Hairy Pixels

On Fri, Jun 28, 2024 at 5:31 PM Hairy Pixels via fpc-pascal <

Show quoted text 6 lines
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.

Hairy Pixels
to Christo Crause
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

Adriaan van Os
to Hairy Pixels: I had a large function which I was profiling with MilliSecondsBetween but I split it to…
Hairy Pixels 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

Hairy Pixels
to Adriaan van Os
Show quoted text 12 lines
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

Michael Van Canneyt
to Hairy Pixels
Hairy Pixels wrote 16 lines
> 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.

Graeme Geldenhuys
to Hairy Pixels: But that's still millisecond precision. Maybe this an OS problem more than anything. I'll…
Hairy Pixels 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

Adriaan van Os
to Michael Van Canneyt: No, these are platform-specific functions. g_get_monotonic_time is probably just an alias…
Michael Van Canneyt wrote 6 lines
> 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>

Regards,

Adriaan van Os

Sven Barth
to Adriaan van Os

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

Show quoted text 16 lines
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

Michael Van Canneyt
to Sven Barth
Sven Barth wrote 23 lines
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.

Adriaan van Os
to Sven Barth: Adriaan van Os via fpc-pascal schrieb am Sa., 29. Juni 2024, 21:21: I wouldn't introduce…
Sven Barth 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