public

  • Download Gist
timestamp_vs_now_concurrent.erl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/usr/bin/env escript
 
epoch() ->
{Mega,Sec,Mili} = now(),
(Mega * 1000000) + Sec + (Mili / 1000000).
 
measure(F) ->
Ini = epoch(),
F(),
epoch() - Ini.
 
waiting() ->
receive
_ -> waiting()
after 500 ->
ok
end.
 
launch(M,F) ->
Times = lists:seq(1,25000),
monitor(process, spawn(fun() -> [ M:F() || _ <- Times ] end)).
 
main(_Args) ->
Times = lists:seq(1,1000),
Test1 = fun() -> [ launch(os, timestamp) || _ <- Times ], waiting() end,
Test2 = fun() -> [ launch(erlang, now) || _ <- Times ], waiting() end,
io:format("os:timestamp = ~5.2f ~5.2f ~5.2f~n", [
measure(Test1), measure(Test1), measure(Test1)
]),
io:format("erlang:now = ~5.2f ~5.2f ~5.2f~n", [
measure(Test2), measure(Test2), measure(Test2)
]),
0.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.