[erlang-questions] Replacing erlang:now/0 for getting a unique number

zxq9 zxq9@REDACTED
Fri Sep 18 09:24:36 CEST 2015


On 2015年9月18日 金曜日 10:04:30 Martin Koroudjiev wrote:
> Hello,
> 
> I have code that users erlang:now/0 to get a unique number. What is the
> alternative in OTP 18? In the documentation for erlang:monotonic_time/0
> says that consecutive calls might produce same results.

It depends on what you are using the value for.

If you are just getting the time, either erlang:system_time/1 (you pass what unit you want), or if you want it to work the way now/0 used to, then use erlang:timestamp/0 -- but this is not guaranteed to be unique (make two calls faster than the clock's resolution, for example).

If you need to determine ordering of events using a timestamp then you need a tuple that combines monotonic_time/0 and inique_integer/1:

  EventStamp = {monotonic_time(), unique_integer([monotonic])}.

Systems are so fast now that outrunning the system clock can actually happen more than you might expect!

There is a section in the release notes for R18 that explains a how time issues have changed.

-Craig



More information about the erlang-questions mailing list