-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiming.hpp
More file actions
47 lines (40 loc) · 932 Bytes
/
timing.hpp
File metadata and controls
47 lines (40 loc) · 932 Bytes
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
#pragma once
#include <ctime>
#include <chrono>
#include <iostream>
namespace timing {
long long secondsSinceEpoch()
{
return std::time(nullptr);
}
class Measurement {
public:
using Clock = std::chrono::steady_clock;
using Time = std::chrono::time_point<Clock>;
using Duration = std::chrono::duration<double>;
private:
Time startTime;
Time endTime;
Clock clock;
public:
void start()
{ startTime = clock.now(); }
void end()
{ endTime = clock.now(); }
double elapsedTime()
{
if (endTime > startTime)
{
auto difference = endTime - startTime;
Duration seconds = std::chrono::duration_cast<Duration>(difference);
return seconds.count();
}
else
{
std::cout << "[ERROR] Time measurement start occured after end."
<< std::endl;
return 0;
}
}
};
}