Create a gist now

Instantly share code, notes, and snippets.

The idea is to create M x N threads, and measure the time cost for every N threads created. After a thread is created, it sends a message to let the main process continue, and finishes execution.
-export([start/2, init/2, thread/1]).
start(Total_Rounds, Step) ->
spawn(?MODULE, init, [Total_Rounds, Step]).
init(Total_Rounds, Step) ->
Start_Time = os:timestamp(),
create_threads(0, 0, Total_Rounds, Step, Start_Time).
create_threads(Total_Rounds, _, Total_Rounds, _, _) ->
create_threads(Round, Step, Total_Rounds, Step, Last_Time) ->
wait_threads(Round, 0, Total_Rounds, Step, Last_Time);
create_threads(Round, Created, Total_Rounds, Step, Last_Time) ->
spawn(?MODULE, thread, [self()]),
create_threads(Round, Created+1, Total_Rounds, Step, Last_Time).
wait_threads(Round, Step, Total_Rounds, Step, Last_Time) ->
Time = os:timestamp(),
Diff = timer:now_diff(Time, Last_Time),
io:format("~p~n", [Diff div 1000]),
create_threads(Round+1, 0, Total_Rounds, Step, Time);
wait_threads(Round, Started, Total_Rounds, Step, Last_Time) ->
started ->
wait_threads(Round, Started+1, Total_Rounds, Step, Last_Time)
thread(Parent) ->
Parent ! started.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment