-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfiltered_sum.cpp
More file actions
92 lines (76 loc) · 2.28 KB
/
filtered_sum.cpp
File metadata and controls
92 lines (76 loc) · 2.28 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* usingstdcpp2015: summing elements over a threshold.
*
* Copyright 2015 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 <algorithm>
#include <array>
#include <chrono>
#include <numeric>
std::chrono::high_resolution_clock::time_point measure_start,measure_pause;
template<typename F>
double measure(F f)
{
using namespace std::chrono;
static const int num_trials=10;
static const milliseconds min_time_per_trial(200);
std::array<double,num_trials> trials;
volatile decltype(f()) res; /* to avoid optimizing f() away */
for(int i=0;i<num_trials;++i){
int runs=0;
high_resolution_clock::time_point t2;
measure_start=high_resolution_clock::now();
do{
res=f();
++runs;
t2=high_resolution_clock::now();
}while(t2-measure_start<min_time_per_trial);
trials[i]=duration_cast<duration<double>>(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)*1E6;
}
template<typename Size,typename F>
double measure(Size n,F f)
{
return measure(f)/n;
}
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 <algorithm>
#include <iostream>
#include <random>
#include <vector>
int main()
{
std::size_t n0=10000,n1=40000000,dn=2000;
double fdn=1.1;
std::cout<<"filtered sum:"<<std::endl;
std::cout<<"n;unsorted;sorted"<<std::endl;
for(std::size_t n=n0;n<=n1;n+=dn,dn=(unsigned int)(dn*fdn)){
std::vector<int> v;
std::mt19937 gen;
std::uniform_int_distribution<> rnd(0,255);
v.reserve(n);
for(std::size_t i=0;i<n;++i)v.push_back(rnd(gen));
auto f=[&](){
long int res=0;
for(int x:v)if(x>128)res+=x;
return res;
};
std::cout<<n<<";";
std::cout<<measure(n,f)<<";";
std::sort(v.begin(),v.end());
std::cout<<measure(n,f)<<"\n";
}
}