-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstopwatch.hpp
39 lines (30 loc) · 858 Bytes
/
stopwatch.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
// Written by Mark Waterman, and placed in the public domain.
// The author hereby disclaims copyright to this source code.
#pragma once
#include <chrono>
namespace mw {
// Stopwatch utility class, start with static StopWatch::start_new().
class StopWatch
{
public:
using Clock = std::chrono::high_resolution_clock;
using time_point = std::chrono::time_point<Clock>;
using duration = std::chrono::duration<double>;
// Returns a new, started stopwatch.
inline static StopWatch start_new()
{
return StopWatch{Clock::now()};
}
inline duration elapsed() const
{
return (Clock::now() - start_time_);
}
inline double elapsed_seconds() const
{
return elapsed().count();
}
private:
time_point start_time_;
StopWatch(time_point start_time) : start_time_(start_time) {}
};
}