Forum > macOS / Mac OS X

High resolution timing - Apple Silicon

<< < (2/3) > >>

Thaddy:
Yes, it is possible. Tomorrow I will knock something up. Dinner time.
You only need pure FreePascal, nothing extra fancy.
(Come to think of it: I asked Jonas months ago for some small changes in the POSIX timer handling. That example will probably work OOTB.)

AlanTheBeast:

--- Quote from: Thaddy on June 24, 2025, 06:46:37 pm ---Yes, it is possible. Tomorrow I will knock something up. Dinner time.
You only need pure FreePascal, nothing extra fancy.
(Come to think of it: I asked Jonas months ago for some small changes in the POSIX timer handling. That example will probably work OOTB.)

--- End quote ---

That would be very kind.  Thank you!
No rush either.

AlanTheBeast:

--- Quote from: gues1 on June 24, 2025, 05:41:26 pm ---For Intel processor I had done that (in assembler) and use it in same cases, for ARM I don't know.
But I think that MacOS with arm still have the QPC (I mean high precision timer) that one can use.

It has to resolution of 0,1 microseconds and works in Hardware, not in software or inside the CPU.

--- End quote ---

The 0.1 µs madness is a Microsoft invention, and IIRC is derived from h/w but scaled to that value in s/w.

It does have one redeeming feature that my very long winded post on GPS v. NTTP time testing gets into:
https://forum.lazarus.freepascal.org/index.php/topic,60420.msg452175.html#msg452175

MarkMLl:
Have you investigated the POSIX clock behaviour, as described at e.g. https://docs.redhat.com/en/documentation/red_hat_enterprise_linux_for_real_time/7/html/reference_guide/sect-posix_clocks ?

My recollection is that that specifies both the units of the syscall, and the best resolution (e.g. nanosec and microsecs respectively).

Now() returning a real number is inherently flawed in that the further you get from the epoch the less your resolution.

As an historical note, my understanding is that the internal clock of ye ancient IBM S/360 defined that on all models a certain bit /would/ be updated at a defined rate (for the sake of argument let's say once per millisecond), and that lesser bits /might/ be updated monotonically.

MarkMLl

Thaddy:
Small example, but it is more complicated. Compile with fpc -k"-framework Cocoa" machtimeexample.pas
But you need to transate the ticks to time with mach_timebase_info(), which I never used, (yet) but this code is the proper timer.
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$modeswitch objectivec1} program MachTimeExample; uses  ctypes, MacOSAll, CocoaAll; var  timebase: mach_timebase_info_data_t;  startTime, endTime, elapsedNano: UInt64; begin  // Initialize the timebase info - necessary to convert ticks to nanoseconds  mach_timebase_info(@timebase);   // Get the start time  startTime := mach_absolute_time();   // Do some work you want to measure  // For example, a small delay  NSProcessInfo.processInfo.sleepForTimeInterval(0.5); // Sleep for 0.5 seconds   // Get the end time  endTime := mach_absolute_time();   // Calculate elapsed time in nanoseconds  elapsedNano := (endTime - startTime) * UInt64(timebase.numer) div UInt64(timebase.denom);   writeln('Start time: ', startTime, ' ticks');  writeln('End time:   ', endTime, ' ticks');  writeln('Elapsed:    ', elapsedNano, ' nanoseconds');  writeln('            ', elapsedNano / 1000000:0:3, ' milliseconds');end.This one is tested, the rest I have to find out....
.....Like you... 8)
My Apple mini is just a toy for these kind of things. I am no expert in that sense.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version