How much time does creating a thread cost? Here are some timings, using
NanoTimer():
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
256 µs for #1/1
521 µs for #2/3
709 µs for #3/4
813 µs for #4/5
970 µs for #5/2
1148 µs for #6/9
1266 µs for #7/11
1407 µs for #8/13
1507 µs for #9/17
1711 µs for #10/7
1862 µs for #11/33
1954 µs for #12/12
2044 µs for #13/27
2071 µs for #14/16
2179 µs for #15/19
2299 µs for #16/10
2370 µs for #17/20
2454 µs for #18/14
2626 µs for #19/6
2710 µs for #20/23
2858 µs for #21/18
2942 µs for #22/21
3048 µs for #23/25
3086 µs for #24/26
3193 µs for #25/29
3264 µs for #26/30
3346 µs for #27/8
3498 µs for #28/37
3590 µs for #29/38
3634 µs for #30/15
3732 µs for #31/28
3772 µs for #32/31
3848 µs for #33/32
3950 µs for #34/35
3985 µs for #35/36
4071 µs for #36/39
4105 µs for #37/40
4366 µs for #38/24
4460 µs for #39/22
4663 µs for #40/34
The first number indicates the order of finished threads (1-40), the second number the order in which they were started.
So
2626 µs for #19/6 means that the thread started in loop iteration #6 finished rather late, as #19.
On average, creating and starting a thread costs slightly less than 120 µs on my machine.
include \masm32\MasmBasic\MasmBasic.inc ; download
.data?
ctThread dd ?
ctMain dd ?
.code
Init
Cls
PrintCpu 0
xor ecx, ecx
.Repeat
inc ecx
.Until Sign?
NanoTimer()
push 39
.Repeat
push eax
inc ctMain
invoke CreateThread, 0, 0, TheThread, ctMain, 0, esp
pop ecx
dec stack
.Until Sign?
pop edx
Delay 100
Exit
TheThread proc arg
mov ecx, NanoTimer(µs)
inc ctThread
Print Str$("%i µs",ecx), Str$("\tfor #%i", ctThread), Str$("/%i\n", arg)
ret
TheThread endp
EndOfCode