纪秀峰]:erlang:now() os:timestamp() 比较

本文对比了Erlang中erlang:now()与os:timestamp()两个时间获取函数的性能,通过实验发现os:timestamp()函数在大量调用时表现出更好的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

纪秀峰]:erlang:now() os:timestamp() 比较
受影响的文件:
    benchmark/src/now_timestamp.erl
commit ceefc934e92fc3ffd66683012d0e306be175302d 1 parent 16cbbe7
jixiuf jixiuf authored January 25, 2013
Show diff stats Hide diff stats
Showing 1 changed file with 32 additions and 0 deletions.
  1. +32 0  benchmark/src/now_timestamp.erl
32  benchmark/src/now_timestamp.erl
View
   @@ -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)
   + .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值