Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High resolution timer in erlang

Tags:

erlang

timer


Does anybody know if it is possible to make a high resolution timer in Erlang?
According to documentation all timers and timeouts are measured in milliseconds.
There is need to make a delay in microseconds. For example, instead of
timer:apply_after(MilliSec, Module, Function, Arguments).
something like
timer:apply_after(MicroSec, Module, Function, Arguments).

like image 667
Grigory Avatar asked Apr 28 '26 19:04

Grigory


1 Answers

Indeed, all timers and timeouts primitives are in milliseconds including :

  • receive ... after primitive (which is what timer module eventually relies upon);
  • erlang:send_after/3 and erlang:start_timer/3 which rely on the same mechanism;
  • driver_set_timer function for linked in drivers.

Two methods could be considered to achieve a sub-millisecond timer:

  • use Erlang primitives to wait the truncated number of milliseconds and then adjust with a busy loop. Please note that erlang:now() is not a real time function as it is guaranteed to be monotonous (and this is quite expensive). You should use os:timestamp() instead;
  • write native code that spawns a thread that will send a message when the timer fires. This could easily be implemented as a NIF.
like image 198
Paul Guyot Avatar answered Apr 30 '26 15:04

Paul Guyot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!