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).
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 :
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With