-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.h
63 lines (52 loc) · 1.7 KB
/
common.h
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
//
// Created by MuyBien on 4/16/16.
//
#ifndef SYSMEASURE_COMMON_H
#define SYSMEASURE_COMMON_H
#include <cstdint>
#include <cmath>
#include <numeric>
#include <vector>
#include <mach/mach_time.h>
using namespace std;
static inline uint64_t rdtscStart(void){
uint32_t cycles_high, cycles_low;
asm volatile (
"CPUID\n\t" /*serialize*/
"RDTSC\n\t" /*read the clock*/
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t"
: "=r" (cycles_high), "=r" (cycles_low):: "%rax", "%rbx", "%rcx", "%rdx", "memory"
);
return (((uint64_t) cycles_high << 32)| cycles_low);
}
static inline uint64_t rdtscEnd(void){
uint32_t cycles_high, cycles_low;
asm volatile (
"RDTSCP\n\t" /*read the clock*/
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t"
"CPUID\n\t"
: "=r" (cycles_high), "=r"(cycles_low):: "%rax", "%rbx", "%rcx", "%rdx", "memory"
);
return (((uint64_t)cycles_high << 32) | cycles_low);
}
static inline vector<double> stats(vector<double> v) {
double sum = std::accumulate(v.begin(), v.end(), 0.0);
double mean = sum / v.size();
double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
double stdev = std::sqrt(sq_sum / v.size() - mean * mean);
double min = *std::min_element(v.begin(), v.end());
double max = *std::max_element(v.begin(), v.end());
cout << "mean: " << mean << endl;
cout << "stdev: " << stdev << endl;
cout << "min: " << min << endl;
cout << "max: " << max << endl;
vector<double> ret;
ret.push_back(mean);
ret.push_back(stdev);
ret.push_back(min);
ret.push_back(max);
return ret;
}
#endif //SYSMEASURE_COMMON_H