erlang:now / 0比os快:timestamp / 0? [英] erlang:now/0 is faster than os:timestamp/0?
问题描述
如手册中所述, http://www.erlang.org/erldoc?q = erlang:now
如果不需要返回值为唯一且单调增加,请使用os:timestamp / 0而不是一些开销。
If you do not need the return value to be unique and monotonically increasing, use os:timestamp/0 instead to avoid some overhead.
os:timestamp / 0应该比erlang快:now / 0
os:timestamp/0 should be faster than erlang:now/0
但是我在计算机上测试了计时器:tc / 3,对于10000000个电话,花费在微秒的时间是:
But I tested on my PC with timer:tc/3, for 10000000 calls, time spent in microsecond is:
erlang:now 951000
os:timestamp 1365000
erlang:now 951000
os:timestamp 1365000
为什么erlang:now / 0比os:timestamp / 0?
Why erlang:now/0 faster than os:timestamp/0?
我的操作系统:Windows 7 x64,erlang版本:R16B01。
My OS: Windows 7 x64, erlang version: R16B01.
----------------编辑-----------------
------------------edit-----------------
我并行编写了另一个测试代码(100个线程),os:timestamp / 0并行执行得更好。这里是数据:
I wrote another test code in parallel (100 thread), os:timestamp/0 performed better in parallel. here are data:
----- single thread ------
erlang:now 95000
os:timestamp 147000
----- multi thread ------
erlang:now 333000
os:timestamp 91000
所以,我认为开销是为了并行。
So, I think the "overhead" is for parallel.
推荐答案
我一直认为一些开销的评论是黑暗的有趣的。方式 erlang:now / 0
实现其提供保证唯一,单调递增值的技巧是取出每个VM全局锁。在一个串行测试中,你不会注意到任何东西,但是当你有很多并行代码运行时,你可以。
I've always thought that the 'some overhead' comment was darkly amusing. The way erlang:now/0
achieves its trick of providing guaranteed unique, monotonically increasing values is to take out a per-VM global lock. In a serial test you won't notice anything, but when you've got a lot of parallel code running, you may.
函数 os:timestamp / 0
不会取出锁,并且可能在两个进程中返回相同的值。
The function os:timestamp/0
doesn't take out a lock and may return the same value in two processes.
这篇关于erlang:now / 0比os快:timestamp / 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!