Stopwatch.h
1.1 KB
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
#pragma once
#include "Time.h"
namespace baselib
{
BASELIB_CPP_INTERFACE
{
// Stopwatch
// Simplistic stopwatch tool to take accurate time measurements using Baselib_Timer
//
// Usage example:
// auto watch = Stopwatch::StartNew();
// HeavyOperation();
// printf("Time passed: %fs", watch.GetElapsedTime().ToSeconds());
class Stopwatch
{
public:
static Stopwatch StartNew() { return Stopwatch(); }
high_precision_clock::duration GetElapsedTime() const
{
return high_precision_clock::duration_from_ticks(high_precision_clock::now_in_ticks() - m_StartTime);
}
high_precision_clock::duration Restart()
{
high_precision_clock::duration elapsed = GetElapsedTime();
m_StartTime = high_precision_clock::now_in_ticks();
return elapsed;
}
private:
Stopwatch() : m_StartTime(high_precision_clock::now_in_ticks()) {}
Baselib_Timer_Ticks m_StartTime;
};
}
}