-
Notifications
You must be signed in to change notification settings - Fork 1
/
functimer.h
64 lines (51 loc) · 1.35 KB
/
functimer.h
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
62
63
64
/*
* Copyright (C) 2016 - 2023 Judd Niemann - All Rights Reserved.
* You may use, distribute and modify this code under the
* terms of the GNU Lesser General Public License, version 2.1
*
* You should have received a copy of GNU Lesser General Public License v2.1
* with this file. If not, please refer to: https://github.com/jniemann66/ReSampler
*/
#ifndef _FUNCTIMER_H
#define _FUNCTIMER_H
#include <iostream>
#include <iomanip>
#include <chrono>
#include <QElapsedTimer>
// class FuncTimer : starts a high-resolution timer upon construction and
// upon destruction, accumulates time spent alive into total_duration
template<typename D>
class FuncTimer
{
double* const duration;
std::chrono::time_point<std::chrono::high_resolution_clock> beginTimer;
std::chrono::time_point<std::chrono::high_resolution_clock> endTimer;
public:
explicit FuncTimer(double* d) : duration(d)
{
beginTimer = std::chrono::high_resolution_clock::now();
}
~FuncTimer()
{
endTimer = std::chrono::high_resolution_clock::now();
if(duration != nullptr) {
*duration = std::chrono::duration_cast<D>(endTimer - beginTimer).count();
}
}
};
// Qt version
class FuncTimerQ
{
QElapsedTimer timer;
double* const duration;
public:
explicit FuncTimerQ(double* d) : duration(d)
{
timer.start();
}
~FuncTimerQ()
{
*duration = timer.nsecsElapsed();
}
};
#endif // _FUNCTIMER_H