Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

erlang:start_timer/3 vs timer:send_after/3

Tags:

erlang

What should I prefer:

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 atom message to a gen_server? I expect to have hundreds of thousands of gen_servers and each one of those will need to have an associated TTL timer event.

like image 500
Maxim Vladimirsky Avatar asked Jul 24 '12 07:07

Maxim Vladimirsky


1 Answers

The Common Caveats section of the Erlang Efficiency Guide says:

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

The functions in the timer module that do not manage timers (such as timer:tc/3 or timer:sleep/1), do not call the timer-server process and are therefore harmless.

like image 143
legoscia Avatar answered Oct 03 '22 21:10

legoscia