Skip to content

Instantly share code, notes, and snippets.

@sasekazu
Created November 6, 2014 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasekazu/827ca7d5dadda0c34ac6 to your computer and use it in GitHub Desktop.
Save sasekazu/827ca7d5dadda0c34ac6 to your computer and use it in GitHub Desktop.
Intel TBB sort time measure
/*
* 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