-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCpuBenchmarker.h
85 lines (75 loc) · 1.99 KB
/
CpuBenchmarker.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* CpuBenchmarker.h
*
* Created on: Feb 21, 2021
* Author: tugrul
*/
#ifndef CPUBENCHMARKER_H_
#define CPUBENCHMARKER_H_
#include <chrono>
#include <string>
#include <iostream>
#include <iomanip>
// RAII type benchmarker
class CpuBenchmarker
{
public:
CpuBenchmarker():CpuBenchmarker(0,"",0)
{
measurementTarget=nullptr;
}
CpuBenchmarker(size_t bytesToBench):CpuBenchmarker(bytesToBench,"",0)
{
measurementTarget=nullptr;
}
CpuBenchmarker(size_t bytesToBench, std::string infoExtra):CpuBenchmarker(bytesToBench,infoExtra,0)
{
measurementTarget=nullptr;
}
CpuBenchmarker(size_t bytesToBench, std::string infoExtra, size_t countForThroughput):t1(std::chrono::duration_cast< std::chrono::nanoseconds >(std::chrono::high_resolution_clock::now().time_since_epoch()))
{
bytes=bytesToBench;
info=infoExtra;
count = countForThroughput;
measurementTarget=nullptr;
}
// writes elapsed time (in seconds) to this variable upon destruction
void addTimeWriteTarget(double * measurement)
{
measurementTarget=measurement;
}
~CpuBenchmarker()
{
std::chrono::nanoseconds t2 = std::chrono::duration_cast< std::chrono::nanoseconds >(std::chrono::high_resolution_clock::now().time_since_epoch());
size_t t = t2.count() - t1.count();
if(measurementTarget!=nullptr)
{
*measurementTarget=t/1000000000.0; // seconds
}
if(info!=std::string(""))
std::cout<<info<<": ";
std::cout<<t<<" nanoseconds ";
if(bytes>0)
{
std::cout <<" (bandwidth = ";
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << (bytes/(((double)t)/1000000000.0))/1000000.0 <<" MB/s) ";
}
if(count>0)
{
std::cout<<" (throughput = ";
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << (((double)t)/count) <<" nanoseconds per iteration) ";
}
std::cout<<std::endl;
}
private:
std::chrono::nanoseconds t1;
size_t bytes;
size_t count;
std::string info;
double * measurementTarget;
};
#endif /* CPUBENCHMARKER_H_ */