0

Which is more costly in terms of performance: a few threads with many context switches or many threads with few context switches?

Assuming that the CPU can process one thread at a time:

In Case 1 with 3 threads and 300 context switches, each thread is context switched an average of about 100 times,

In Case 2 with 300 threads and 1 context switch per thread, the total number of switches is also 300.

So which one is more performance-intensive?"

nnv98
  • 1
  • 1
  • Benchmark the two implementations on your hardware and find out. – Andrew Henle Jun 04 '23 at 16:53
  • 1
    Most number-crunching problems have some overhead to distribute work to threads, so using way more threads than you have hardware cores is usually bad. (Some problems are easier, e.g. mandelbrot fractals, vs. harder like matmul or video encoding.) Also for cache locality. Creating a thread also takes some CPU time in OS system calls. Also, how would you know ahead of time that you'll get exactly 1 context switch for each thread? I guess if they each do about 15 ms of work, and your OS uses a 10 ms timeslice for scheduling, that's a likely guess on a single-core CPU. – Peter Cordes Jun 04 '23 at 22:02

0 Answers0