-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionTimer.cpp
93 lines (75 loc) · 2.06 KB
/
FunctionTimer.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "FunctionTimer.h"
#include <stdio.h>
#include <chrono>
#include <time.h>
#include <string.h>
using namespace std::chrono;
FILE *log_file=nullptr;
void log(const char* s)
{
if (log_file == nullptr)
return;
struct timespec ts;
timespec_get(&ts, TIME_UTC);
char buff[100];
strftime(buff, sizeof buff, "%T", gmtime(&ts.tv_sec));
fprintf(log_file, "%s.%09ld %s\n", buff, ts.tv_nsec, s);
fflush(log_file);
}
int FunctionTimer::MAX_NESTING=10;
struct FunctionTimerImpl
{
FunctionTimerImpl(const char* func_name) :
m_func_name(func_name)
{
t1 = high_resolution_clock::now();
m_nesting_count = nesting_count;
nesting_count++;
}
~FunctionTimerImpl()
{
nesting_count--;
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
// if (m_nesting_count < FunctionTimer::MAX_NESTING)
// printf("%s() took %f seconds.\n", m_func_name, time_span.count());
//fflush(log_file);
}
static int nesting_count ;
int m_nesting_count;
const char* m_func_name;
high_resolution_clock::time_point t1;
};
int FunctionTimerImpl::nesting_count = 0;
FunctionTimer::FunctionTimer(const char *func_name)
{
impl = new FunctionTimerImpl(func_name);
}
FunctionTimer::~FunctionTimer()
{
delete impl;
}
struct TestFunctionTimerImpl
{
TestFunctionTimerImpl(const char* func_name):
m_func_name(func_name)
{
t1 = high_resolution_clock::now();
}
~TestFunctionTimerImpl()
{
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
printf("%s() took %f seconds.\n", m_func_name, time_span.count());
}
const char* m_func_name;
high_resolution_clock::time_point t1;
};
TestFunctionTimer::TestFunctionTimer(const char *func_name)
{
impl = new TestFunctionTimerImpl(func_name);
}
TestFunctionTimer::~TestFunctionTimer()
{
delete impl;
}