Author Topic: CreateThread overhead  (Read 5714 times)

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
CreateThread overhead
« on: January 31, 2021, 12:56:21 PM »
How much time does creating a thread cost? Here are some timings, using NanoTimer():

Code: [Select]
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.

Code: [Select]
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

quarantined

  • Regular Member
  • *
  • Posts: 22
Re: CreateThread overhead
« Reply #1 on: January 31, 2021, 06:25:20 PM »
Code: [Select]
AMD A6-9220e RADEON R4, 5 COMPUTE CORES 2C+3G  1.60 GHz
-2147483648 µs for #2/1
171 µs for #1/2
290 µs for #3/3
351 µs for #4/5
405 µs for #5/4
476 µs for #6/7
546 µs for #7/9
594 µs for #8/6
642 µs for #9/8
678 µs for #10/10
714 µs for #11/11
771 µs for #12/12
844 µs for #13/13
909 µs for #14/15
955 µs for #15/14
963 µs for #16/17
1031 µs for #17/16
1081 µs for #18/18
1126 µs for #19/19
1145 µs for #20/20
1204 µs for #21/21
1270 µs for #22/23
1377 µs for #23/24
1411 µs for #24/31
1454 µs for #25/22
1534 µs for #26/25
1588 µs for #27/27
1672 µs for #28/29
1695 µs for #29/40
1727 µs for #30/33
1775 µs for #31/28
1813 µs for #33/32
1840 µs for #34/34
1872 µs for #35/35
1792 µs for #32/30
1929 µs for #36/37
2011 µs for #37/36
2070 µs for #38/26
2087 µs for #39/39
2156 µs for #40/38

-2147483648 µs   for #2/1 ??

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: CreateThread overhead
« Reply #2 on: January 31, 2021, 08:37:46 PM »
-2147483648 µs   for #2/1 ??

Lovely :tongue:

It means that the first call to QueryPerformanceFrequency returned zero. Can you reproduce it?

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: CreateThread overhead
« Reply #3 on: February 01, 2021, 01:11:39 AM »
Here is a variant: CreateThread once, then the thread gets suspended and resumed. Typical timings:
Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GH
1851 µs for creating the thread
thread resumed after 280 µs    for #1
thread resumed after 2 µs       for #2
thread resumed after 10 µs      for #3
thread resumed after 4 µs       for #4
thread resumed after 7 µs       for #5
thread resumed after 20 µs      for #6
thread resumed after 2 µs       for #7
thread resumed after 6 µs       for #8
thread resumed after 80 µs      for #9
thread resumed after 14 µs      for #10
thread resumed after 6 µs       for #11
thread resumed after 4 µs       for #12
thread resumed after 8 µs       for #13
thread resumed after 2 µs       for #14
thread resumed after 10 µs      for #15
thread resumed after 6 µs       for #16
thread resumed after 17 µs      for #17
thread resumed after 14 µs      for #18
thread resumed after 4 µs       for #19
thread resumed after 4 µs       for #20
thread resumed after 9 µs       for #21
thread resumed after 11 µs      for #22
thread resumed after 18 µs      for #23
thread resumed after 17 µs      for #24
thread resumed after 46 µs      for #25
thread resumed after 21 µs      for #26
thread resumed after 22 µs      for #27
thread resumed after 44 µs      for #28
thread resumed after 32 µs      for #29
thread resumed after 20 µs      for #30
thread resumed after 56 µs      for #31

daydreamer

  • Member
  • *****
  • Posts: 1721
  • building nextdoor
Re: CreateThread overhead
« Reply #4 on: February 01, 2021, 05:44:32 AM »
first exe wont work correctly,it just quickly runs and ends,without any way to see timings or any text
second exe :
Code: [Select]
Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
137 µs for creating the thread
thread resumed after 994 µs     for #1
thread resumed after 26 µs      for #2
thread resumed after 43 µs      for #3
thread resumed after 25 µs      for #4
thread resumed after 21 µs      for #5
thread resumed after 76 µs      for #6
thread resumed after 23 µs      for #7
thread resumed after 22 µs      for #8
thread resumed after 76 µs      for #9
thread resumed after 52 µs      for #10
thread resumed after 52 µs      for #11
thread resumed after 61 µs      for #12
thread resumed after 89 µs      for #13
thread resumed after 53 µs      for #14
thread resumed after 63 µs      for #15
thread resumed after 53 µs      for #16
thread resumed after 56 µs      for #17
thread resumed after 65 µs      for #18
thread resumed after 55 µs      for #19
thread resumed after 29 µs      for #20
thread resumed after 47 µs      for #21
thread resumed after 29 µs      for #22
thread resumed after 31 µs      for #23
thread resumed after 33 µs      for #24
thread resumed after 17 µs      for #25
thread resumed after 72 µs      for #26
thread resumed after 28 µs      for #27
thread resumed after 49 µs      for #28
thread resumed after 88 µs      for #29
thread resumed after 56 µs      for #30
thread resumed after 93 µs      for #31
--- hit any key ---
new fashion to measure performance µs
so have clock cycles measuring become vintage?  :tongue:
SIMD fan and macro fan
why assembly is fastest is because its switch has no (brakes) breaks
:P
only in 16bit assembly you can get away with "Only words" :P

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: CreateThread overhead
« Reply #5 on: February 01, 2021, 07:48:48 AM »
first exe wont work correctly,it just quickly runs and ends,without any way to see timings or any text

You can run it from a DOS prompt.

Quote
so have clock cycles measuring become vintage?  :tongue:

Yessss. Cycles don't mean anything between threads.

quarantined

  • Regular Member
  • *
  • Posts: 22
Re: CreateThread overhead
« Reply #6 on: February 02, 2021, 12:47:28 PM »
It means that the first call to QueryPerformanceFrequency returned zero. Can you reproduce it?

AMD A6-9220e RADEON R4, 5 COMPUTE CORES 2C+3G 
-2147483648 µs   for #2/1
171 µs   for #1/2
290 µs   for #3/3
351 µs   for #4/5
405 µs   for #5/4
476 µs   for #6/7
546 µs   for #7/9
594 µs   for #8/6
642 µs   for #9/8
678 µs   for #10/10
714 µs   for #11/11
771 µs   for #12/12
844 µs   for #13/13
909 µs   for #14/15
955 µs   for #15/14
963 µs   for #16/17
1031 µs   for #17/16
1081 µs   for #18/18
1126 µs   for #19/19
1145 µs   for #20/20
1204 µs   for #21/21
1270 µs   for #22/23
1377 µs   for #23/24
1411 µs   for #24/31
1454 µs   for #25/22
1534 µs   for #26/25
1588 µs   for #27/27
1672 µs   for #28/29
1695 µs   for #29/40
1727 µs   for #30/33
1775 µs   for #31/28
1813 µs   for #33/32
1840 µs   for #34/34
1872 µs   for #35/35
1792 µs   for #32/30
1929 µs   for #36/37
2011 µs   for #37/36
2070 µs   for #38/26
2087 µs   for #39/39
2156 µs   for #40/38

 :cool:

quarantined

  • Regular Member
  • *
  • Posts: 22
Re: CreateThread overhead
« Reply #7 on: February 02, 2021, 12:51:03 PM »
for single thread.....:
Code: [Select]
AMD A6-9220e RADEON R4, 5 COMPUTE CORES 2C+3G 
66 µs for creating the thread
thread resumed after 101 µs for #1
thread resumed after 21 µs for #2
thread resumed after 12 µs for #3
thread resumed after 8 µs for #4
thread resumed after 15 µs for #5
thread resumed after 14 µs for #6
thread resumed after 17 µs for #7
thread resumed after 14 µs for #8
thread resumed after 14 µs for #9
thread resumed after 14 µs for #10
thread resumed after 15 µs for #11
thread resumed after 21 µs for #12
thread resumed after 19 µs for #13
thread resumed after 20 µs for #14
thread resumed after 22 µs for #15
thread resumed after 31 µs for #16
thread resumed after 20 µs for #17
thread resumed after 14 µs for #18
thread resumed after 16 µs for #19
thread resumed after 20 µs for #20
thread resumed after 19 µs for #21
thread resumed after 15 µs for #22
thread resumed after 28 µs for #23
thread resumed after 16 µs for #24
thread resumed after 19 µs for #25
thread resumed after 20 µs for #26
thread resumed after 17 µs for #27
thread resumed after 16 µs for #28
thread resumed after 21 µs for #29
thread resumed after 17 µs for #30
thread resumed after 15 µs for #31

2nd run...

Code: [Select]
AMD A6-9220e RADEON R4, 5 COMPUTE CORES 2C+3G 
87 µs for creating the thread
thread resumed after 189 µs for #1
thread resumed after 30 µs for #2
thread resumed after 194 µs for #3
thread resumed after 24 µs for #4
thread resumed after 17 µs for #5
thread resumed after 22 µs for #6
thread resumed after 26 µs for #7
thread resumed after 24 µs for #8
thread resumed after 17 µs for #9
thread resumed after 33 µs for #10
thread resumed after 22 µs for #11
thread resumed after 19 µs for #12
thread resumed after 17 µs for #13
thread resumed after 19 µs for #14
thread resumed after 20 µs for #15
thread resumed after 33 µs for #16
thread resumed after 19 µs for #17
thread resumed after 34 µs for #18
thread resumed after 17 µs for #19
thread resumed after 17 µs for #20
thread resumed after 15 µs for #21
thread resumed after 17 µs for #22
thread resumed after 20 µs for #23
thread resumed after 16 µs for #24
thread resumed after 21 µs for #25
thread resumed after 17 µs for #26
thread resumed after 15 µs for #27
thread resumed after 17 µs for #28
thread resumed after 17 µs for #29
thread resumed after 17 µs for #30
thread resumed after 20 µs for #31
--- hit any key ---

quarantined

  • Regular Member
  • *
  • Posts: 22
Re: CreateThread overhead
« Reply #8 on: February 02, 2021, 12:53:41 PM »
first exe wont work correctly,it just quickly runs and ends,without any way to see timings or any text


or use a batch file with   >output.txt to print it
 :tongue:

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: CreateThread overhead
« Reply #9 on: February 02, 2021, 01:56:43 PM »
Thanks :thumbsup:

TimoVJL

  • Member
  • ****
  • Posts: 725
Re: CreateThread overhead
« Reply #10 on: February 02, 2021, 06:15:11 PM »
Windows 10
Code: [Select]
AMD Ryzen 5 3400G with Radeon Vega Graphics
70 µs for creating the thread
thread resumed after 188 µs     for #1
thread resumed after 13 µs      for #2
thread resumed after 9 µs       for #3
thread resumed after 25 µs      for #4
thread resumed after 9 µs       for #5
thread resumed after 10 µs      for #6
thread resumed after 15 µs      for #7
thread resumed after 9 µs       for #8
thread resumed after 14 µs      for #9
thread resumed after 10 µs      for #10
thread resumed after 8 µs       for #11
thread resumed after 10 µs      for #12
thread resumed after 11 µs      for #13
thread resumed after 8 µs       for #14
thread resumed after 10 µs      for #15
thread resumed after 8 µs       for #16
thread resumed after 9 µs       for #17
thread resumed after 8 µs       for #18
thread resumed after 13 µs      for #19
thread resumed after 8 µs       for #20
thread resumed after 36 µs      for #21
thread resumed after 9 µs       for #22
thread resumed after 31 µs      for #23
thread resumed after 64 µs      for #24
thread resumed after 9 µs       for #25
thread resumed after 174 µs     for #26
thread resumed after 9 µs       for #27
thread resumed after 12 µs      for #28
thread resumed after 13 µs      for #29
thread resumed after 270 µs     for #30
thread resumed after 9 µs       for #31
--- hit any key ---
May the source be with you

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: CreateThread overhead
« Reply #11 on: February 02, 2021, 08:17:10 PM »
 :thumbsup:

My initial CreateThread is always above 1000 µs, on Win7-64. I wonder if Win10 has become better, or if it's cpu related :rolleyes:

TimoVJL

  • Member
  • ****
  • Posts: 725
Re: CreateThread overhead
« Reply #12 on: February 02, 2021, 08:34:24 PM »
Windows 7 x64
Code: [Select]
AMD Athlon(tm) II X2 220 Processor
111 µs for creating the thread
thread resumed after 335 µs     for #1
thread resumed after 16 µs      for #2
thread resumed after 92 µs      for #3
thread resumed after 56 µs      for #4
thread resumed after 16 µs      for #5
thread resumed after 25 µs      for #6
thread resumed after 337 µs     for #7
thread resumed after 16 µs      for #8
thread resumed after 20 µs      for #9
thread resumed after 23 µs      for #10
thread resumed after 7 µs       for #11
thread resumed after 17 µs      for #12
thread resumed after 53 µs      for #13
thread resumed after 35 µs      for #14
thread resumed after 61 µs      for #15
thread resumed after 114 µs     for #16
thread resumed after 128 µs     for #17
thread resumed after 13 µs      for #18
thread resumed after 20 µs      for #19
thread resumed after 8 µs       for #20
thread resumed after 153 µs     for #21
thread resumed after 84 µs      for #22
thread resumed after 220 µs     for #23
thread resumed after 15 µs      for #24
thread resumed after 27 µs      for #25
thread resumed after 12 µs      for #26
thread resumed after 227 µs     for #27
thread resumed after 129 µs     for #28
thread resumed after 9 µs       for #29
thread resumed after 10 µs      for #30
thread resumed after 125 µs     for #31
--- hit any key ---
May the source be with you

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8496
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: CreateThread overhead
« Reply #13 on: February 02, 2021, 11:51:17 PM »
The old i7 is clocked at 4 gig.

Intel(R) Core(TM) i7-5820K CPU @ 3.30GHz
152 µs  for #1/1
182 µs  for #2/2
420 µs  for #3/3
444 µs  for #4/4
464 µs  for #5/5
489 µs  for #6/6
512 µs  for #7/7
551 µs  for #8/8
571 µs  for #9/9
591 µs  for #10/10
620 µs  for #11/11
649 µs  for #12/12
666 µs  for #13/13
684 µs  for #14/14
705 µs  for #15/15
736 µs  for #16/28
786 µs  for #17/17
838 µs  for #18/32
868 µs  for #19/19
889 µs  for #20/20
923 µs  for #21/21
951 µs  for #22/22
997 µs  for #23/23
1040 µs for #24/37
1060 µs for #25/25
1092 µs for #26/26
1135 µs for #27/27
1176 µs for #28/16
1214 µs for #29/29
1234 µs for #30/30
1281 µs for #31/18
1320 µs for #32/33
1359 µs for #33/34
1392 µs for #34/35
1431 µs for #35/36
1467 µs for #36/24
1489 µs for #37/38
1528 µs for #38/39
1569 µs for #39/40
1619 µs for #40/31
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8496
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: CreateThread overhead
« Reply #14 on: February 02, 2021, 11:58:57 PM »
12 Core Xeon.

Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz
180 µs  for #1/1
214 µs  for #2/2
478 µs  for #3/5
525 µs  for #4/4
557 µs  for #5/3
599 µs  for #6/6
630 µs  for #7/20
666 µs  for #8/8
696 µs  for #9/21
719 µs  for #10/23
749 µs  for #11/11
774 µs  for #12/25
796 µs  for #13/26
827 µs  for #14/14
856 µs  for #15/15
897 µs  for #16/16
913 µs  for #17/17
959 µs  for #18/18
1005 µs for #19/33
1053 µs for #20/7
1079 µs for #21/36
1105 µs for #22/9
1128 µs for #23/38
1187 µs for #24/24
1234 µs for #25/12
1288 µs for #26/13
1342 µs for #27/27
1421 µs for #28/28
1454 µs for #29/29
1523 µs for #30/30
1594 µs for #31/32
1645 µs for #32/31
1671 µs for #33/19
1748 µs for #34/34
1797 µs for #35/37
1842 µs for #36/22
1891 µs for #37/39
1916 µs for #38/10
1989 µs for #39/40
2124 µs for #40/35

hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: