Code runs 6 times slower with 2 threads than with 1

Posted by Edward Bird on Stack Overflow See other posts from Stack Overflow or by Edward Bird
Published on 2013-06-27T16:16:21Z Indexed on 2013/06/27 16:21 UTC
Read the original article Hit count: 266

So I have written some code to experiment with threads and do some testing.

The code should create some numbers and then find the mean of those numbers.

I think it is just easier to show you what I have so far. I was expecting with two threads that the code would run about 2 times as fast. Measuring it with a stopwatch I think it runs about 6 times slower!

void findmean(std::vector<double>*, std::size_t, std::size_t, double*);


int main(int argn, char** argv)
{

// Program entry point
std::cout << "Generating data..." << std::endl;

// Create a vector containing many variables
std::vector<double> data;
for(uint32_t i = 1; i <= 1024 * 1024 * 128; i ++) data.push_back(i);

// Calculate mean using 1 core
double mean = 0;
std::cout << "Calculating mean, 1 Thread..." << std::endl;
findmean(&data, 0, data.size(), &mean);
mean /= (double)data.size();

// Print result
std::cout << "  Mean=" << mean << std::endl;

// Repeat, using two threads
std::vector<std::thread> thread;
std::vector<double> result;
result.push_back(0.0);
result.push_back(0.0);
std::cout << "Calculating mean, 2 Threads..." << std::endl;

// Run threads
uint32_t halfsize = data.size() / 2;
uint32_t A = 0;
uint32_t B, C, D;
// Split the data into two blocks
if(data.size() % 2 == 0)
{
    B = C = D = halfsize;
}
else if(data.size() % 2 == 1)
{
    B = C = halfsize;
    D = hsz + 1;
}

// Run with two threads
thread.push_back(std::thread(findmean, &data, A, B, &(result[0])));
thread.push_back(std::thread(findmean, &data, C, D , &(result[1])));

// Join threads
thread[0].join();
thread[1].join();

// Calculate result
mean = result[0] + result[1];
mean /= (double)data.size();

// Print result
std::cout << "  Mean=" << mean << std::endl;

// Return
return EXIT_SUCCESS;
}


void findmean(std::vector<double>* datavec, std::size_t start, std::size_t length, double* result)
{
for(uint32_t i = 0; i < length; i ++) {
    *result += (*datavec).at(start + i);
}
}

I don't think this code is exactly wonderful, if you could suggest ways of improving it then I would be grateful for that also.

© Stack Overflow or respective owner

Related posts about c++

Related posts about multithreading