Erlang: start_timer / 3 vs timer: send_after / 3

What I need:

erlang:start_timer(Ttl, self(), time_to_die) 

or

 timer:send_after(Ttl, self(), {timeout, time_to_die}) 

if my use case is to send a single message with the gen_server atom? I expect you to have hundreds of thousands of gen_servers, and each of them will need to bind a TTL timer event.

+6
source share
2 answers

The Erlang Performance Guidance General Warnings section reads:

Creating timers using erlang:send_after/3 and erlang:start_timer/3 much more efficient than using timers provided by the timer module. The timer module uses a separate process to manage timers, and this process can easily be overloaded if many processes often create and cancel timers (especially when using the SMP emulator).

Functions of the timer module that do not control timers (for example, timer:tc/3 or timer:sleep/1 ) do not cause the timer-server process and therefore are harmless.

+20
source

Since I donโ€™t know exactly what your preferences are, I assume that you are asking which one is faster when I have n processes:

Not knowing 100%, I do not think that this will be of great importance. If you have hundreds or thousands of instances of gen_server, I would advise you to spend time optimizing them. If you still need to chase microseconds while delivering messages, you can always try and analyze profiling to see which one is faster.

-2
source

Source: https://habr.com/ru/post/921178/


All Articles