timer:tc/3, erlang:now/0 documentation
István Lõrincz
istvan.lorincz@REDACTED
Fri Jun 18 15:25:13 CEST 2010
Hi!
I think the documentation of timer:tc/3 is misleading, because it states, that the function returns the real execution time of the specified MFA. In reality it returns a duration based on virtual time returned by erlang:now/0.
On the other hand, the documentation of erlang:now/0 is misleading too somewhat, because it states that it returns the POSIX time, without mentioning that it's true only in case you don't make repetitive calls to erlang:now/0 in a shorter time than 1 microsec.
To illustrate this I wrote the following code (tested on Windows Vista Enterprise SP1, Erlang OTP R13B04, Sun SPARC Solaris 5.8, Erlang OTP R11B):
----------------------------------------------------------------
-module(now).
-compile(export_all).
now_test(0)->
ok;
now_test(Counter) ->
now(),%%generating unique time-stamps
now_test(Counter-1).
test(Amount)->
NowStartTime = calendar:now_to_datetime(now()),
SysStartTime = calendar:local_time(),
{TCRunTime, _Result} = timer:tc( now, now_test, [Amount]),
SysRunTime = calendar:time_difference(SysStartTime,
calendar:local_time()),
NowTime = calendar:now_to_datetime(now()),
SysTime = calendar:local_time(),
io:format("calendar:local_time(): ~p~n",[SysStartTime]),
io:format("calendar:now_to_datetime(now()): ~p~n",[NowStartTime]),
io:format("Run time by timer:tc( now, now_test, [Amount]): ~p~n", [TCRunTime]),
io:format("Run time by calendar:local_time(): ~p~n",[SysRunTime]),
io:format("calendar:local_time(): ~p~n",[SysTime]),
io:format("calendar:now_to_datetime(now()): ~p~n",[NowTime]).
----------------------------------------------------------------
332> erlang:system_info(otp_release).
"R13B04"
333> now:test(10000000).
calendar:local_time(): {{2010,6,17},{12,5,23}}
calendar:now_to_datetime(now()): {{2010,6,17},{10,5,23}} Run time by timer:tc( now, now_test, [Amount]): 10000001 Run time by calendar:local_time(): {0,{0,0,2}}
calendar:local_time(): {{2010,6,17},{12,5,25}}
calendar:now_to_datetime(now()): {{2010,6,17},{10,5,33}}
334>
----------------------------------------------------------------
2> erlang:system_info(otp_release).
"R11B"
3> now:test(10000000).
calendar:local_time(): {{2010,6,17},{12,42,45}}
calendar:now_to_datetime(now()): {{2010,6,17},{10,42,45}} Run time by timer:tc( now, now_test, [Amount]): 10000004 Run time by calendar:local_time(): {0,{0,0,7}}
calendar:local_time(): {{2010,6,17},{12,42,52}}
calendar:now_to_datetime(now()): {{2010,6,17},{10,42,55}} ok
4>
Regards,
István
More information about the erlang-questions
mailing list