Created
November 6, 2014 03:08
-
-
Save sasekazu/827ca7d5dadda0c34ac6 to your computer and use it in GitHub Desktop.
Intel TBB sort time measure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* tbbTest.cpp | |
* | |
* Created on: 2014/11/05 | |
* Author: sase | |
*/ | |
#include <iostream> | |
#include <math.h> | |
#include <sys/time.h> | |
#include <vector> | |
#include <algorithm> | |
#include <numeric> | |
#include "tbb/task_scheduler_init.h" | |
#include "tbb/parallel_sort.h" | |
using std::cout; | |
using std::endl; | |
using std::vector; | |
double gettimeofday_sec(){ | |
struct timeval t; | |
gettimeofday(&t, NULL); | |
return (double)t.tv_sec + (double)t.tv_usec * 1e-6; | |
} | |
void SortExample(const int n) { | |
const int itrmax = 100; | |
vector<int> a(n), b(n); | |
vector<double> timeSTL, timeTBB; | |
srand((unsigned) time(NULL)); | |
for(int itr=0; itr<itrmax; ++itr){ | |
// Set random array | |
std::generate(a.begin(), a.end(), [](){ return rand()%100;}); | |
std::copy(a.begin(), a.end(), b.begin()); | |
double tstart = gettimeofday_sec(); | |
// TBB | |
tbb::parallel_sort(a.data(), a.data() + n); | |
double tend = gettimeofday_sec(); | |
// STL | |
std::sort(b.begin(), b.end()); | |
double tendSTL = gettimeofday_sec(); | |
timeTBB.push_back(tend-tstart); | |
timeSTL.push_back(tendSTL-tend); | |
} | |
cout << n << " " | |
<< std::accumulate(timeTBB.begin(), timeTBB.end(), 0.0)/(double)itrmax << " " | |
<< std::accumulate(timeSTL.begin(), timeSTL.end(), 0.0)/(double)itrmax << endl; | |
} | |
int main(){ | |
int size = 100000; | |
for(int p=1; p<=6; ++p){ | |
tbb::task_scheduler_init init(p); | |
SortExample(size); | |
} | |
// for(int size=10000; size<=500000; size+=10000){ | |
// SortExample(size); | |
// } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment