Boost logo

Boost :

From: Lance.Diduck_at_[hidden]
Date: 2008-05-14 15:57:25

The result has been noticed before. In fact I wrote about it in several
posts on c.l.c++.m, and in N2486 in the first few pages it uses this
effect to illustrate a point about allocators.
Nevertheless, the result is still surprising!!
What is worse is that different compilers and libraries will give you
way different results for this test, so it is not just a function of
Intel. ( I have not tried to reproduce the results on anything but
It is quite common that completely independent algorithms on a multicore
machine will slow each other down --worse than just running the same
algorithms sequentially. Of course we can make programs go faster by
parallelism, but the theoretical approach of making many isolated
simultaneous tasks clearly does not meet practice as a way to improve
So forget about "superlinear" programs. I would just settle for C++
containers being linear.

-----Original Message-----
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of James Sutherland
Sent: Wednesday, May 14, 2008 3:32 PM
To: boost_at_[hidden]
Subject: [boost] very poor thread performance

I have been testing thread performance on Linux and Mac. My Linux
system has two dual-core processors and my Mac has one dual-core
processor. Both are intel chips.

For the code snippet given below, the execution time should ideally
decrease as the number of threads increases. However, the opposite
trend is observed. For example, using -O3 flags on my Linux desktop
produces the following timings:
1 Thread: 0.66 sec
2 Threads: 0.9 sec
3 Threads: 1.2 sec
4 Threads: 1.4 sec

I do not have a lot of experience with threads, and was wondering if
this result surprises anyone?


#include <boost/thread.hpp>
#include <iostream>
#include <vector>
#include <time.h>

struct MyStruct
   explicit MyStruct(const int i) : tag(i) {}
   void operator()() const
     const int n = 100;
     std::vector<int> nums(n,0);
     for( int j=0; j<1000000; ++j )
       for( int i=0; i<n; ++i )
         nums[i] = i+tag;
   int tag;

int main()
   using namespace std;
   const int nTasks = 12;
   const int nThreads = 4;
   assert( nTasks%nThreads == 0 );
   assert( nThreads<=nTasks );
   cout << "Executing " << nTasks << " using " << nThreads << "
threads." << endl;
   time_t t1 = clock();
   for( int itask=0; itask<nTasks; ++itask ){
     boost::thread_group threads;
     for( int i=0; i<nThreads; ++i ){
       threads.create_thread( MyStruct(itask++ + 100) );
   t2 = clock();
   cout << "time: " << difftime( t2,t1)/CLOCKS_PER_SEC << endl; }

Unsubscribe & other changes:
Visit our website at
This message contains confidential information and is intended only 
for the individual named.  If you are not the named addressee you 
should not disseminate, distribute or copy this e-mail.  Please 
notify the sender immediately by e-mail if you have received this 
e-mail by mistake and delete this e-mail from your system.
E-mails are not encrypted and cannot be guaranteed to be secure or 
error-free as information could be intercepted, corrupted, lost, 
destroyed, arrive late or incomplete, or contain viruses.  The sender 
therefore does not accept liability for any errors or omissions in the 
contents of this message which arise as a result of e-mail transmission.  
If verification is required please request a hard-copy version.  This 
message is provided for informational purposes and should not be 
construed as a solicitation or offer to buy or sell any securities 
or related financial instruments.

Boost list run by bdawes at, gregod at, cpdaniel at, john at