Repository navigation
Expand file tree
/
Copy pathTimeProfile.cpp
More file actions
110 lines (94 loc) · 3.86 KB
/
Copy pathTimeProfile.cpp
File metadata and controls
110 lines (94 loc) · 3.86 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Metagenomics Canopy Clustering Implementation
*
* Copyright (C) 2013, 2014 Piotr Dworzynski (piotr@cbs.dtu.dk), Technical University of Denmark
*
* This file is part of Metagenomics Canopy Clustering Implementation.
*
* Metagenomics Canopy Clustering Implementation is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Metagenomics Canopy Clustering Implementation is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TimeProfile.hpp"
#include <algorithm>
#include <iomanip>
#include <vector>
TimeProfile::TimeProfile()
: profile_started_at(ProfileClock::now()) {
}
std::string TimeProfile::format_duration_hhmmss(std::int64_t seconds) {
if (seconds < 0) {
seconds = 0;
}
const std::int64_t hours = seconds / 3600;
const std::int64_t minutes = (seconds % 3600) / 60;
const std::int64_t secs = seconds % 60;
std::ostringstream formatted;
formatted << std::setfill('0')
<< std::setw(2) << hours << ":"
<< std::setw(2) << minutes << ":"
<< std::setw(2) << secs;
return formatted.str();
}
std::int64_t TimeProfile::total_elapsed() const {
return std::chrono::duration_cast<std::chrono::seconds>(
ProfileClock::now() - profile_started_at).count();
}
void TimeProfile::start_timer(string timer_name){
if(timers.count(timer_name) && timers[timer_name].running)
throw "Timer is already running";
timers[timer_name] = TimerState{true, ProfileClock::now(), 0};
}
void TimeProfile::restart_timer(string timer_name){
if(!timers.count(timer_name))
throw "Timer doesn't exist";
timers[timer_name] = TimerState{true, ProfileClock::now(), 0};
}
void TimeProfile::stop_timer(string timer_name){
if((!timers.count(timer_name)) || (!timers[timer_name].running))
throw "Timer doesn't exist or was already stopped";
TimerState& timer = timers[timer_name];
timer.elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>(
ProfileClock::now() - timer.started_at).count();
timer.running = false;
_log(logINFO) << timer_name << ": "
<< format_duration_hhmmss(timer.elapsed_seconds)
<< " | total passed: " << format_duration_hhmmss(total_elapsed());
}
std::ostream& operator<<(std::ostream& ost, const TimeProfile& tp)
{
ost << "Timing summary (HH:MM:SS; total passed: "
<< TimeProfile::format_duration_hhmmss(tp.total_elapsed())
<< "):" << std::endl;
std::vector<std::string> timer_names;
timer_names.reserve(tp.timers.size());
for (const TimerMapType::value_type& timer_name_value_pair : tp.timers) {
timer_names.push_back(timer_name_value_pair.first);
}
std::sort(timer_names.begin(), timer_names.end());
for (const std::string& timer_name : timer_names) {
const auto timer_it = tp.timers.find(timer_name);
const bool timer_running = timer_it->second.running;
std::int64_t timer_time = timer_it->second.elapsed_seconds;
if (timer_running) {
timer_time = std::chrono::duration_cast<std::chrono::seconds>(
ProfileClock::now() - timer_it->second.started_at).count();
}
ost << " - " << timer_name << ": "
<< TimeProfile::format_duration_hhmmss(timer_time);
if (timer_running) {
ost << " (running)";
}
ost << std::endl;
}
return ost;
}