[erlang-questions] os:perf_counter/1 is slow

Lukas Larsson <>
Wed Aug 10 11:33:28 CEST 2016


The conversion to micro seconds has not been optimized as much for
os:perf_counter/1 as it has for the other functions which is why there is
such a large difference. If I use os:perf_counter() instead I get about
1.3-1.4. The remaining 30%-40% comes from the fact that bignums are
returned by os:perf_counter() and not smalls.

I did a quick hack to see what the result would be if os:perf_counter/1
used the same optimization as monotonic/system_time and then the time is
about the same for all four time measurement variants. On modern hw running
modern OS's the gain of os:perf_counter over erlang:system_time will not be
all that great.

On Wed, Aug 10, 2016 at 3:29 AM, Michael Truog <> wrote:

> Hi,
>
> When I test the speed of os:perf_counter/1 as a potential source of
> pseudo-randomness from the microseconds result, the speed is slower than
> os:timestamp/0, erlang:monotonic_time/0 and erlang:system_time/1 which I
> found surprising, since the documentation claims os:perf_counter/1 should
> be fast (http://erlang.org/doc/man/os.html#perf_counter-1).  Should this
> be explained as a hardware problem due to lower-level instruction usage
> that is hardware dependent, or is this still being improved (it is new in
> 19.0)?
>
> The results from https://github.com/okeuday/erlbench when I do "make" is:
> N == 10000 (10 runs)
>          18_bxor_abs get:     2507.1 us (  1.2)
> 18_erlang:system_tim get:     2095.7 us (  1.0)
>         18_monotonic get:     2186.1 us (  1.0)
>      18_rand_exs1024 get:    13539.0 us (  6.5)
>        18_rand_exs64 get:    11205.1 us (  5.3)
>      18_rand_exsplus get:     6171.4 us (  2.9)
> 19_os:perf_counter/1 get:     5214.4 us (  2.5)
> crypto:rand_uniform/ get:    88909.9 us ( 42.4)
>       os:timestamp/0 get:     2214.8 us (  1.1)
>     random:uniform/1 get:     6058.7 us (  2.9)
> random_wh06_int:unif get:    14252.9 us (  6.8)
> random_wh82:uniform/ get:     6049.0 us (  2.9)
> random_wh82_int:unif get:     6595.8 us (  3.1)
>
> The machine is:
> Core i7 2670QM 2.2GHz 1 cpu, 4 cores/cpu, 2 hts/core
> L2:4×256KB L3:6MB RAM:8GB:DDR3-1333MHz
> Sandy Bridge-HE-4 (Socket G2)
>
> Thanks,
> Michael
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160810/3329fb3a/attachment.html>


More information about the erlang-questions mailing list