-
Notifications
You must be signed in to change notification settings - Fork 0
/
measureSpentTime.hpp
45 lines (42 loc) · 1.5 KB
/
measureSpentTime.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
#ifndef _MEASURE_SPENT_TIME_HPP_
#define _MEASURE_SPENT_TIME_HPP_
#include <chrono>
#include <functional>
enum class MeasureTimeType{
NanoSeconds,
MicroSeconds,
MilliSeconds,
Seconds,
Minutes,
Hours
};
template<class Functor,class... TArgs>
auto measureSpentTime(MeasureTimeType measureTimeType,Functor func,TArgs&&... args)->std::chrono::time_point<std::chrono::high_resolution_clock>::rep{
auto start = std::chrono::high_resolution_clock::now();
func(std::forward<TArgs>(args)...);
auto end=std::chrono::high_resolution_clock::now();
auto spentTime=-1LL;
switch (measureTimeType)
{
case MeasureTimeType::NanoSeconds:
spentTime=std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();
break;
case MeasureTimeType::MicroSeconds:
spentTime=std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
break;
case MeasureTimeType::MilliSeconds:
spentTime=std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
break;
case MeasureTimeType::Seconds:
spentTime=std::chrono::duration_cast<std::chrono::seconds>(end-start).count();
break;
case MeasureTimeType::Minutes:
spentTime=std::chrono::duration_cast<std::chrono::minutes>(end-start).count();
break;
case MeasureTimeType::Hours:
spentTime=std::chrono::duration_cast<std::chrono::hours>(end-start).count();
break;
}
return spentTime;
}
#endif