forked from ncrookston/cpp-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope_timer.hpp
62 lines (54 loc) · 1.19 KB
/
scope_timer.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef UTILITY_SCOPETIMER_HPP
#define UTILITY_SCOPETIMER_HPP
#include <iostream>
#ifdef _MSC_VER
//These trainings target VC10 and above, so no std::chrono :(
# include <windows.h>
#else
# include <chrono>
#endif
class scope_timer
{
public:
#ifdef _MSC_VER
scope_timer()
: m_startTime()
{
QueryPerformanceCounter(&m_startTime);
}
#else
typedef std::chrono::high_resolution_clock Clock_t;
scope_timer()
: m_start(Clock_t::now())
{}
#endif
#ifdef _MSC_VER
~scope_timer()
{
LARGE_INTEGER endTime;
QueryPerformanceCounter(&endTime);
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
double time = static_cast<double>(
endTime.QuadPart - m_startTime.QuadPart) / frequency.QuadPart;
std::cout << "Duration: " << time << " seconds" << std::endl;
}
#else
~scope_timer()
{
Clock_t::time_point stop = Clock_t::now();
const double t =
1e-9 * std::chrono::duration_cast<
std::chrono::nanoseconds
>(stop - m_start).count();
std::cout << "Duration: " << t << " seconds" << std::endl;
}
#endif
private:
#ifdef _MSC_VER
LARGE_INTEGER m_startTime;
#else
const Clock_t::time_point m_start;
#endif
};
#endif