[erlang-questions] now/0 resolution

Raimo Niskanen <>
Mon Sep 10 13:26:42 CEST 2007


On Fri, Sep 07, 2007 at 07:23:34AM -0500, Serge Aleynikov wrote:
> Since according to documentation consecutive executions of now/0 will 
> return different values (with microsecond precision) I have two questions:
> 
> 1. Does it mean that inserting now/0 calls in code will slow down 
> execution to 1mks per execution?  If not, then performance of function 
> call measured using now/0 time stamping would be inaccurate.
> 

Well, no on both alternatives. The return value from now/0 is bumped
with 1 us for every call to always return an incrementing value. But
it is hardly a problem - it will not be noticabely inaccurate.

To make now/0 off by more than 1 us you will have to call it so often,
more than once per us, that it becomes impractical.

With faster and faster computers it might become a problem one day.



To time a function call you also can use call trace with
timestamps. Then on some platforms (Solaris (and Linux?))
you have the option to count CPU time instead.

> 2. Is it possible to measure execution time more accurately (with 
> nanosecond precision) using current Erlang distribution? (*)
> 
> Serge
> 
> (*) Since VM uses gettimeofday() to bump timer, it's limited by 
> getimeofday's precision.  Additionally each invocation of gettimeofday 
> takes about 15mks (OS dependent).  I believe that there's no code to 
> take advantage of a high resolution timer.  Such a timer can give 

The high resolution timer is actually used for timekeeping by now/0
and tracing, but the nanoseconds are not available in any API.

> nanosecond precision, and presently the only way it can be implemented 
> is in a linked-in driver.  The only problem that calling functions 
> inside the driver with erlang:port_call/3 incurs some cost that would be 
> better to avoid if a bif (such as hrnow/0) was available.
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list