Created
December 6, 2018 02:14
-
-
Save taiyow/6d0dffaf534722eee83c9ccfee3a0a67 to your computer and use it in GitHub Desktop.
1msのtimerをビジーループで実現
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(busytimer_test). | |
-export([test/2]). | |
-define(MICROSECONDS, 1000000). | |
test(Wait, Count) -> | |
Begin = erlang:timestamp(), | |
busywait_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]). | |
busywait_repeat(_Wait, 0) -> | |
ok; | |
busywait_repeat(Wait, N) -> | |
busywait(Wait), | |
busywait_repeat(Wait, N-1). | |
busywait(Wait) -> | |
Finish = erlang:monotonic_time(?MICROSECONDS) + Wait*1000, | |
busywait_until(Finish). | |
busywait_until(Finish) -> | |
Now = erlang:monotonic_time(?MICROSECONDS), | |
if Now >= Finish -> ok; | |
true -> busywait_until(Finish) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
erl上で以下を実行:
だいたい秒間1000回になっている。