Note that there are some explanatory texts on larger screens.

plurals
  1. POCPU speed and threads in C++
    primarykey
    data
    text
    <p>I have the following C++ program:</p> <pre><code>void testSpeed(int start, int end) { int temp = 0; for(int i = start; i &lt; end; i++) { temp++; } } int main() { using namespace boost; timer aTimer; // start two new threads that calls the "testSpeed" function boost::thread my_thread1(&amp;testSpeed, 0, 500000000); boost::thread my_thread2(&amp;testSpeed, 500000000, 1000000000); // wait for both threads to finish my_thread1.join(); my_thread2.join(); double elapsedSec = aTimer.elapsed(); double IOPS = 1/elapsedSec; } </code></pre> <p>So the idea is to test the CPU speed in terms of integer operations per second (IOPS). There are 1 billion iterations (operations), so on 1Ghz CPU we should get around billion integer operations per second, I believe. </p> <p>My assumption is that more threads = more integer operations per second. But the more threads I try the less operations per second I see (I have more cores than threads). What may causing such a behavior? Is it the threads overhead? Maybe I should try a much longer experiment to see if the threads actually help?</p> <p>Thank you!</p> <p><strong>UPDATE</strong>: So I changed the loop to run 18 billions times, and declared temp as volatile. Also added another testSpeed method with different name so now a single threaded executes both methods one after another, while two threads get each one method; so there shouldn't be any sync' issues, etc. And... still no change in behavior! single threaded is faster according to timer. Ahhh! I found the sucker, apparently timer is bluffing. The two threads take half the time to finish but timer tells me the single threaded run was two seconds faster. I'm now trying to understand why... Thanks everyone!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload