-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathtimer.hpp
More file actions
35 lines (27 loc) · 816 Bytes
/
timer.hpp
File metadata and controls
35 lines (27 loc) · 816 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
#pragma once
#include <chrono>
#include <iostream>
class Timer {
public:
Timer()
: start_(std::chrono::high_resolution_clock::now()),
end_(std::chrono::high_resolution_clock::now()) {}
void reset() { start_ = std::chrono::high_resolution_clock::now(); }
void stop() { end_ = std::chrono::high_resolution_clock::now(); }
double elapsedSeconds() const {
auto timeSpan = std::chrono::duration_cast<std::chrono::duration<double>>(
end_ - start_);
return timeSpan.count();
}
private:
std::chrono::high_resolution_clock::time_point start_;
std::chrono::high_resolution_clock::time_point end_;
};
class ScopedTimer : public Timer {
public:
ScopedTimer() {}
~ScopedTimer() {
stop();
std::cout << "Elapsed: " << elapsedSeconds() << " s" << std::endl;
}
};