Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this extra thread make it faster than unthreaded?

I just had a (to me) very odd observation and want to know how this can be. I tested the following two versions of code:

chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
process_data(l, 8);
chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
cout << "time used: " << time_span.count() << endl;

vs

chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
thread t1 = thread(process_data, l, 8);
t1.join();
chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
cout << "time used: " << time_span.count() << endl;

For reasons I don't understand, the second version is 20% faster...

How can this be? The chrono::steady_clock should measure the time correctly, I think... But then I fail to see how creating another thread, and waiting for it can actually be faster then doing it with the initial thread. What am I missing?

Some details: there is no code besides the definition of l before above posted snippets, and no other calculations comes after it (it is the main function) and process_data() is just a massive number-cruncher, including some file-reading operations (no threads used there).

like image 867
Mahrgell Avatar asked May 06 '14 11:05

Mahrgell


1 Answers

The only overhead you got is the thread creation , since your main thread will just sleep until the join.

The thread creation overhead is meaningless compared to your process_data , considering that your program takes 7,7 or 6,5 seconds to run.

So your question can now become : How come a worker thread is faster then the main thread ?

There are many reasons why this could happen , couple that go through my mind :

  • When you are creating the new thread he gets lucky and ends up on a core all by itself
  • There are watchers added by the OS/other programs on your main thread -> which result in the comp running slower as a whole when your main thread isn't idle

The OS / other programs usually go after the main thread of a process for communication, watching etc. so it's not unusual for a main thread to be slower then a worker thread for big data processing.

Even if the thread is a higher priority thread, it doesn't guarantee that things on that thread will move faster.

Here's another similar question: Why main thread is slower than worker thread in pthread-win32?

like image 73
MichaelCMS Avatar answered Oct 29 '22 08:10

MichaelCMS