-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.cc
81 lines (69 loc) · 1.78 KB
/
timer.cc
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
#include "timer.h"
Timer::Timer()
{
duration_ = 0.0;
is_running_ = false;
}
void Timer::Start()
{
if (is_running_)
return;
is_running_ = true;
gettimeofday(&start_time_, NULL);
}
double Timer::Stop()
{
if (!is_running_)
return 0.0;
is_running_ = false;
timeval endTime;
long seconds, useconds;
gettimeofday(&endTime, NULL);
seconds = endTime.tv_sec - start_time_.tv_sec;
useconds = endTime.tv_usec - start_time_.tv_usec;
double last_duration = (seconds + useconds/1000000.0) * 1000;
duration_ += last_duration;
return last_duration;
}
double Timer::Reset()
{
Stop();
double old_duration = duration_;
duration_ = 0.0;
return old_duration;
}
double Timer::GetDuration()
{
return duration_;
}
map<string, Timer> TimerPool::timer_map_;
void TimerPool::Start(const string& timer_name)
{
TimerPool::timer_map_[timer_name].Start();
}
double TimerPool::Stop(string const &timer_name)
{
return TimerPool::timer_map_[timer_name].Stop();
}
double TimerPool::Reset(string const &timer_name)
{
return TimerPool::timer_map_[timer_name].Reset();
}
void TimerPool::Print(const string& timer_name)
{
double last_duration = TimerPool::timer_map_[timer_name].Stop();
double total_duration = TimerPool::timer_map_[timer_name].GetDuration();
LOG_INFO("Duration of [" << timer_name << "] in ms: " <<
"last " << last_duration << " , total " << total_duration);
}
void TimerPool::PrintAll()
{
map<string, Timer>::iterator it;
double duration;
for (it = timer_map_.begin(); it != timer_map_.end(); it++) {
it->second.Stop();
duration = it->second.GetDuration();
LOG_INFO("Total duration of ["<< it->first <<
"] in ms: " << duration);
}
}