Skip to content

Instantly share code, notes, and snippets.

@taiyow
Created December 6, 2018 01:29
Show Gist options
  • Save taiyow/5aa2343f203287b8ea7d75f029f8ff7c to your computer and use it in GitHub Desktop.
Save taiyow/5aa2343f203287b8ea7d75f029f8ff7c to your computer and use it in GitHub Desktop.
1msのtimerで実質2ms待ちになる調査
-module(timer_test).
-export([test/2]).
test(Wait, Count) ->
Begin = erlang:timestamp(),
wait_repeat(Wait, Count),
End = erlang:timestamp(),
DiffUsec = timer:now_diff(End, Begin),
DiffSec = DiffUsec / 1000 / 1000,
io:format("~p counts in ~p sec, ~p count/sec~n", [Count, DiffSec, Count/DiffSec]).
wait_repeat(_Wait, 0) ->
ok;
wait_repeat(Wait, N) ->
timer:sleep(Wait),
wait_repeat(Wait, N-1).
@taiyow
Copy link
Author

taiyow commented Dec 6, 2018

erl 上で以下で実行。

1> c(timer_test).
{ok,timer_test}
2> timer_test:test(1,1000).
1000 counts in 2.001901 sec, 499.52520129616795 count/sec
ok

@jj1bdx
Copy link

jj1bdx commented Dec 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment