-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.h
31 lines (27 loc) · 792 Bytes
/
Timer.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
#pragma once
#ifndef TIMER_H
#define TIMER_H
#include <chrono>
typedef std::chrono::high_resolution_clock::time_point TimeVar;
#define duration(a) std::chrono::duration_cast<std::chrono::milliseconds>(a).count()
#define timeNow() std::chrono::high_resolution_clock::now()
template<typename F, typename... Args>
double functionTime(F func, Args&&... args) {
TimeVar t1 = timeNow();
func(std::forward<Args>(args)...);
TimeVar t2 = timeNow();
auto total = duration(t2 - t1);
return static_cast<double>(total);
}
template<typename E, typename... Args>
double measureFunctionTime(int loop, E func, Args&&... args) {
if (loop == 0) return 0;
int count = 0;
double total = 0;
while (count < loop) {
total += functionTime(E,args);
count++;
}
return total / (double)(loop);
}
#endif