-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathperfutils.hpp
60 lines (49 loc) · 1.13 KB
/
perfutils.hpp
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
#pragma once
#include <map>
#include <sys/time.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
namespace emphf {
inline double get_time_usecs() {
struct timeval tv;
gettimeofday(&tv, NULL);
return double(tv.tv_sec) * 1000000 + double(tv.tv_usec);
}
// stolen from folly
template <class T>
inline void do_not_optimize_away(T&& datum) {
asm volatile("" : "+r" (datum));
}
struct stats_accumulator {
stats_accumulator()
: m_n(0)
, m_mean(0)
, m_m2(0)
{}
void add(double x)
{
m_n += 1;
auto delta = x - m_mean;
m_mean += delta / m_n;
m_m2 += delta * (x - m_mean);
}
double mean() const
{
return m_mean;
}
double variance() const
{
return m_m2 / (m_n - 1);
}
double relative_stddev() const
{
return std::sqrt(variance()) / mean() * 100;
}
private:
double m_n;
double m_mean;
double m_m2;
};
}