|
Boost : |
Subject: Re: [boost] [accumulator][minmax_element] Perfomance comparsion different min max algorithm
From: Hansi (hansipet_at_[hidden])
Date: 2009-02-16 13:25:43
Eric Niebler schrieb:
>
> Did you turn on compiler optimizations? If not, the results are
> meaningless. If you did, please post the full source code. What you
> posted was incomplete.
sorry..yes I have turned on compiler optimization.
#include <algorithm>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <boost/algorithm/minmax_element.hpp>
#include <boost/array.hpp>
#include <boost\chrono\chrono.hpp>
using namespace boost;
using namespace boost::chrono;
using namespace std;
typedef array<int,10000000> dataarray;
dataarray data;
int _tmain(int argc, _TCHAR* argv[])
{
high_resolution_clock::time_point start = high_resolution_clock::now();
high_resolution_clock::time_point stop = high_resolution_clock::now();
for(unsigned int i = 0; i< data.size();i++)
{
data[i] = i;
}
start = high_resolution_clock::now();
dataarray::iterator resmin = std::min_element(data.begin(),data.end());
dataarray::iterator resmax = std::max_element(data.begin(),data.end());
stop = high_resolution_clock::now();
cout<<"min_element,max_element:"<<duration_cast<milliseconds>(stop-start).count()<<endl;
cout<<"minVal="<<*resmin<<",maxVal="<<*resmax<<endl;
boost::accumulators::accumulator_set<int,
boost::accumulators::features<boost::accumulators::tag::min,boost::accumulators::tag::max>>
acc;
start = high_resolution_clock::now();
acc = std::for_each(data.begin(),data.end(),acc);
stop = high_resolution_clock::now();
int minVal = boost::accumulators::extract_result<
boost::accumulators::tag::min >(acc);
int maxVal = boost::accumulators::extract_result<
boost::accumulators::tag::max >(acc);
cout<<"min_max:
accumulator:"<<duration_cast<milliseconds>(stop-start).count()<<endl;
cout<<"minVal="<<minVal<<",maxVal="<<maxVal<<endl;
start = high_resolution_clock::now();
pair< vector<int>::iterator, vector<int>::iterator > result =
boost::minmax_element(data.begin(), data.end());
stop = high_resolution_clock::now();
cout<<"min_max:
minmax_element:"<<duration_cast<milliseconds>(stop-start).count()<<endl;
cout<<"minVal="<<*result.first<<",maxVal="<<*result.second<<endl;
minVal = (std::numeric_limits<int>::max)();
maxVal = (std::numeric_limits<int>::min)();
start = high_resolution_clock::now();
for(dataarray::iterator it = data.begin(); it!=data.end();it++)
{
minVal = (std::min)(minVal,*it);
maxVal = (std::max)(maxVal,*it);
}
stop = high_resolution_clock::now();
cout<<"min/max
handcoded:"<<duration_cast<milliseconds>(stop-start).count()<<endl;
cout<<"minVal="<<minVal<<",maxVal="<<maxVal<<endl;
return 0;
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk