/* Performance comparison of plain vs. segmented for_each loops * over double_ended::batch_deque. * * Copyright 2017 Joaquin M Lopez Munoz. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) */ #include #include #include #include #include std::chrono::high_resolution_clock::time_point measure_start,measure_pause; template double measure(F f) { using namespace std::chrono; static const int num_trials=10; static const milliseconds min_time_per_trial(200); std::array trials; volatile decltype(f()) res; /* to avoid optimizing f() away */ for(int i=0;i>(t2-measure_start).count()/runs; } (void)res; /* var not used warn */ std::sort(trials.begin(),trials.end()); return std::accumulate( trials.begin()+2,trials.end()-2,0.0)/(trials.size()-4); } template double measure(unsigned int n,F f) { double t=measure(f); return (t/n)*10E9; } void pause_timing() { measure_pause=std::chrono::high_resolution_clock::now(); } void resume_timing() { measure_start+=std::chrono::high_resolution_clock::now()-measure_pause; } #include #include #include #include #include template void measure_for_each(F f) { std::cout<<"n\tplain\tsegmented\n"; for(std::size_t n=1000;n<=10000000;n*=10){ boost::double_ended::batch_deque< int,boost::double_ended::batch_deque_policy> d; for(auto i=n;i--;)d.push_back(i); unsigned long res=0; auto f1=[&](){ res=0; std::for_each(d.begin(),d.end(),[&](int x){res+=f(x);}); return res; }; auto f2=[&](){ res=0; for(auto it=d.segment_begin(),end=d.segment_end();it!=end;++it){ std::for_each( it.data(),it.data()+it.data_size(),[&](int x){res+=f(x);}); } return res; }; std::cout<<"10E"<(F1); std::cout<<"segment size: "<(F1); std::cout<<"\n"; std::cout<(F2); std::cout<<"segment size: "<(F2); }