Hello All,
I was experimenting with creating a large number of threads and checking how fast it runs on my machine.(details provided later)
This is the code i'm using:
#include <iostream>
#include <thread>
#include <ctime>
#define THREADED 1
static const int num_threads = 100;
static int mult_array[100][1000000];
void multResult(int *arr)
{
for (int i = 0; i < 1000000; i++)
*(arr+(1000000-1)) += (*arr+i);
}
int main()
{
std::thread t[num_threads];
for (int i = 0; i < 100; i++)
for (int j = 0; j < 1000000; j++)
mult_array[i][j] = 1;
clock_t begin = clock();
{
#ifdef THREADED
for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(multResult, mult_array[i]);
}
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
clock_t end = clock();
double traceTime = double(end - begin) / CLOCKS_PER_SEC;
std::cout << "\nTime:" << traceTime;
#else
for (int i = 0; i < 100; i++)
multResult(mult_array[i]);
clock_t end = clock();
double traceTime = double(end - begin) / CLOCKS_PER_SEC;
std::cout << "\nTime:" << traceTime;
#endif
}
return 0;
}
I was assuming the threaded version to be at least faster than the non-threaded version. But, i see the same time reported! About
0.23 seconds. Since i have multiple cores on my machine, i was expecting it to be much faster.
I'm not sure if my measurement is wrong, but i'm confused as to why the times are almost the same(sometimes the threaded version
actually slightly slower!)
Could anyone please point out what i'm doing wrong?
Thanks!
Details:
Processor: Intel i5 2300(quad core)
OS: Ubuntu 12.04
Compiler: GCC 4.6.3