[erlang-questions] perform a function on a fixed time interval
Jon Meredith
jon@REDACTED
Tue Aug 24 16:49:41 CEST 2010
On 8/24/10 5:31 AM, Max Lapshin wrote:
> On Tue, Aug 24, 2010 at 3:26 PM, Miguel Morales<therevoltingx@REDACTED> wrote:
>> I'll keep that in mind. Didn't know it was expensive. Thanks.
>>
>> erlang:statistics(wallclock) may be lighter, if you use it 1000 times per second
>>
>> ________________________________________________________________
>> erlang-questions (at) erlang.org mailing list.
>> See http://www.erlang.org/faq.html
>> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
I did a little microbenchmarking on OSX to look at the three
date/timestamp options I know about. It turns out that
statistics(wallclock) takes a very similar amount of time to run as
erlang:now(), but os:timestamp() is almost twice as fast (without any of
the guarantees of now/0).
now()/statistics(wallclock) ~ 6.5 million calls/sec
os:timestamp() ~ 11.7 million calls/sec
*NB* I didn't measure concurrent performance of the three calls so there
are no effects from locks measured.
Also be careful of using timer:tc() when measuring code with many calls
to now(). Each call to now() increments the nanosecs to make sure you
get a unique value per node. If you call now() fast enough it affects
your timings. I wrote equivalents of timer:tc for the other two time
measuring methods and they align fairly well.
%% Erlang time mechanism micro-benchmarks (8-core 3Ghz Macpro2,1, R13B04
64-bit)
%%
%% Conclusion: now()/statistics(wallclock) take about the same amount
of time
%% os:timestamp() runs almost twice as fast as the other two
%% using timer:tc on code that calls now() a lot is unreliable
%%
%% 35> bench:tstc(bench, do_now, [10000000]).
%% {1537955,ok}
%% 36> bench:wctc(bench, do_now, [10000000]).
%% {1542000,ok}
%% 37> bench:tstc(bench, do_timestamp, [10000000]).
%% {853846,ok}
%% 38> bench:wctc(bench, do_timestamp, [10000000]).
%% {857000,ok}
%% 39> bench:tstc(bench, do_wallclock, [10000000]).
%% {1515972,ok}
%% 40> bench:wctc(bench, do_wallclock, [10000000]).
%% {1525000,ok}
-module(bench).
-compile([export_all]).
do_now(0) ->
ok;
do_now(N) ->
now(),
do_now(N-1).
do_wallclock(0) ->
ok;
do_wallclock(N) ->
erlang:statistics(wall_clock),
do_wallclock(N-1).
do_timestamp(0) ->
ok;
do_timestamp(N) ->
os:timestamp(),
do_timestamp(N-1).
wctc(M, F, A) ->
{S,_} = erlang:statistics(wall_clock),
R = apply(M,F,A),
{E,_} = erlang:statistics(wall_clock),
{(E - S) * 1000, R}.
tstc(M, F, A) ->
S = os:timestamp(),
R = apply(M,F,A),
E = os:timestamp(),
{timer:now_diff(E, S), R}.
More information about the erlang-questions
mailing list