Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with time slicing

I was trying to see the effect of time slicing. And how it can consume significant amount of time. Actually, I was trying to divide a certain work into number of threads and see the effect.

I have a two core processor. So two threads can run in parallel. I was trying to see if I have a work w that is done by 2 threads, and if I have the same work done by t threads with each thread doing w/t of the work. How much does time slicing play a role in it

As time slicing is time consuming process, I was expecting that when I do the same work using a two thread process or by a t thread process, the amount of time taken by the t thread process will be more

However, I found that it was not the case. I tried with t=10. And still it is faster than the 2 thread process. For eg. if I have to do 10,000,000 iterations, with the two thread process I will have the 2 threads do iterations for 5,000,000 so that we have a total of 10,000,000 iterations. If I have to do with the 10 thread process, I will let each thread do iterations for 1,000,000 so that we have a total of 10,000,000 as well.

I was expecting the 10 thread process to consume more time. But it's not the case. Is there any bug in the code? It looks fine to me

Any suggestions?

like image 877
user12331 Avatar asked Nov 03 '22 17:11

user12331


1 Answers

For an app to demonstrate a significant, easily-measurable slowdown with many more threads than processors, you have to work at it:

1) The threads must be CPU-intensive, ie. not block on I/O or each other. If you are using a simple counting loop, (as it sounds like you are), then yes, that's done.

2) You have to arrange each thread to work on data that is large enough so that the L1 cache requires significant flushing upon a context-swap. If you just increment one integer, this flushing will not happen and the context-switch overhead will be too small, (compared with the interval between timer-driven scheduling runs), to easily demonstrate.

Windows example data, minimal cache-flushing, i7, 4/8 cores:

8 tests,
400 tasks,
counting to 10000000,
using 8 threads:
Ticks: 2199
Ticks: 2184
Ticks: 2215
Ticks: 2153
Ticks: 2200
Ticks: 2215
Ticks: 2200
Ticks: 2230
Average: 2199 ms

8 tests,
400 tasks,
counting to 10000000,
using 32 threads:
Ticks: 2137
Ticks: 2121
Ticks: 2153
Ticks: 2138
Ticks: 2137
Ticks: 2121
Ticks: 2153
Ticks: 2137
Average: 2137 ms

8 tests,
400 tasks,
counting to 10000000,
using 128 threads:
Ticks: 2168
Ticks: 2106
Ticks: 2184
Ticks: 2106
Ticks: 2137
Ticks: 2122
Ticks: 2106
Ticks: 2137
Average: 2133 ms

8 tests,
400 tasks,
counting to 10000000,
using 400 threads:
Ticks: 2137
Ticks: 2153
Ticks: 2059
Ticks: 2153
Ticks: 2168
Ticks: 2122
Ticks: 2168
Ticks: 2138
Average: 2137 ms
like image 54
Martin James Avatar answered Nov 15 '22 04:11

Martin James