纪秀峰]:erlang:now() os:timestamp() 比较
受影响的文件:
benchmark/src/now_timestamp.erl
Show diff stats
Showing 1 changed file with
32 additions and
0 deletions.
| @@ -0,0 +1,32 @@ | ||
| +%%%------------------------------------------------------------------- | ||
| +%%% @author 纪秀峰 <jixiuf@gmail.com> | ||
| +%%% @doc | ||
| +%%% | ||
| +%%% @end | ||
| +%%% Created : 2013-01-25 10:21 by 纪秀峰 <jixiuf@gmail.com> | ||
| +%%%------------------------------------------------------------------- | ||
| +-module(now_timestamp). | ||
| +-export([test/1]). | ||
| + | ||
| +%% 测erlang:now() 与os:timestamp() 哪个速度更快 | ||
| +%% (emacs@jf.org)22> now_timestamp:test(10000). | ||
| +%% erlang:now():{940,ok} | ||
| +%% os:timestamp():{522,ok} | ||
| +%% 测试结果显示 os:timestamp() 较快 | ||
| + | ||
| +test(N)-> | ||
| + T=timer:tc(fun () -> now_benchmark(N) end), | ||
| + io:format("erlang:now():~p~n",[T]) , | ||
| + T2=timer:tc(fun () -> os_timestamp_benchmark(N) end), | ||
| + io:format("os:timestamp():~p~n",[T2]) | ||
| + . | ||
| +now_benchmark(0)->ok; | ||
| +now_benchmark(N)-> | ||
| + now(), | ||
| + now_benchmark(N-1) | ||
| + . | ||
| +os_timestamp_benchmark(0)->ok; | ||
| +os_timestamp_benchmark(N)-> | ||
| + os:timestamp(), | ||
| + os_timestamp_benchmark(N-1) | ||
| + . |
本文对比了Erlang中erlang:now()与os:timestamp()两个时间获取函数的性能,通过实验发现os:timestamp()函数在大量调用时表现出更好的效率。
814

被折叠的 条评论
为什么被折叠?



