1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <vector>
#include <numeric>
#include <iostream>
#include <ctime>
using namespace std;
inline double get_time() { return double(clock())/CLOCKS_PER_SEC; }
int main()
{
typedef int value_type;
vector<value_type> X(10000000,1);
double t0=0;
value_type *psum = new value_type; // alloué dynamiquement pour dupper l'optimisation d'ICL
for (int i=0; i<50; ++i)
{
value_type sum=0;
int imax=X.size();
for (int i=0; i<imax; ++i)
sum+=X[i];
*psum=sum;
}
cout << "C =" << *psum << ": " << get_time()-t0 << "s" << endl;
for (int i=0; i<50; ++i)
{
value_type sum=0;
int imax=X.size();
#pragma omp parallel for schedule(dynamic,100000) reduction(+:sum)
for (int i=0; i<imax; ++i)
sum+=X[i];
*psum=sum;
}
cout << "OpenMP=" << *psum << ": " << get_time()-t0 << "s" << endl;
t0=get_time();
for (int i=0; i<50; ++i)
*psum=accumulate(X.begin(),X.end(),0);
cout << "STL =" << *psum << ": " << get_time()-t0 << "s" << endl;
} |