Portable way to measure time without calling the OS

Hi, does anyone know of a portable way to measure time that doesn't involve calling to the OS ?

I need to use it in a real-time thread for measuring how long is taking my process to compute some audio. Approximations are ok.

I was thinking on measuring CPU cycles and converting them into time by applying some factor that could be calculated during the application initialization.

Thanks

You can use core::arch::x86::_rdtsc for x86 and core::arch::x86_64::_rdtsc for x86_64.

There is no way that is both portable and without involving the OS.

If your code runs without an OS, then you can access hardware-specific timers yourself.

If your code runs under an OS, use OS's APIs for this. In modern OS with multitasking and memory protection anything else may be unreliable. For example, the OS may deny you direct access to Intel's TSC.

3 Likes

Is there something similar to core::arch::x86::_rdtsc for ARM ?

That's the closest thing I could find with ARM in C

You could use inline asm. core::arch::arm and core::arch::aarch64 are unstable anyway. If you want to keep it stable, your only option is to write a C function and call it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.