[erlang-questions] Lockfree now()

Erik Søe Sørensen <>
Fri Jul 13 18:42:51 CEST 2012


Den 13/07/2012 14.00 skrev "Wojtek Narczyński" <>:
>
> On 07/13/2012 10:35 AM, Erik Søe Sørensen wrote:
>>
>> Oh, but you do have a choice between more than one time source in Erlang:
>> now() - as known, for microsecond-precision and uniqueness
>> erlang:universaltime() and friends - for second-precision and YMD-HMS
format.
>> os:timestamp() - for microsecond-precision but without uniqueness and
time correction.
>>
>
> I didn't know that. Not that I ever looked thoroughly...
I used to wonder if it existed... until someone told me. It's probably just
not a module you start looking in for that kind of functionality (at least
when you already know about now()).
I guess a crosslink in the module docs would be an idea?

>> For many places where you could use now() but simply need a timestamp
for logging or the like, os:timestamp() might be more appropriate and a bit
more lightweight. Its return type is compatible with that of now().
>
>
> I just replaced calendar:now_to_datetime(erlang:now()) with
erlang:universaltime() in logging code and gained 13% performance. And that
machine is only dual-core.

Nice! :-)

(Topper: That's nothing, though - try using gregorian-seconds-to-datetime
on a huge number, like MAX_DOUBLE - that can be quite slow...)
Time conversion is one of those things you don't want to do more than
strictly necessary.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120713/aaad8337/attachment.html>


More information about the erlang-questions mailing list