[erlang-questions] Thread creation benchmark
Thu Jan 10 12:59:25 CET 2013
On 10 Jan 2013, at 05:05, Xiao Jia wrote:
> 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.
Why do you make the main thread wait for each thread to respond that it has started? Aren't you therefore measuring the time it takes for the communication with to the 'creator' to take place as well in this case? That's not measuring process creation time, it's measuring that *plus* the time it takes to enqueue a response and for the main process to dequeue that message, plus of course the effect that scheduling has on all of this.
The expression `[spawn(fun() -> (receive stop -> ok end) end) || _ <- lists:seq(1, N)]` will measuring creation time only. You can use timer:tc and the value of N to calculate the average cost.
Spawning is essentially an asynchronous operation, and the semantics aren't defined except that you get back a process identifier once the process is *alive*. The use of erlang:spawn/1 therefore *does* measure the cost of spawning a process properly, thus timer:tc with spawn/1 is a reasonable way to measure this I think.
More information about the erlang-questions