-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathminiprofiler.cpp
107 lines (94 loc) · 3.46 KB
/
miniprofiler.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2012, Olav Stetter
//
// This file is part of TE-Causality.
//
// TE-Causality 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 3 of the License, or
// (at your option) any later version.
//
// TE-Causality 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 TE-Causality. If not, see <http://www.gnu.org/licenses/>.
#include "miniprofiler.h"
MiniProfiler::MiniProfiler() {
// std::cout <<"DEBUG: MiniProfiler#init."<<std::endl;
}
MiniProfiler::~MiniProfiler() {
// std::cout <<"DEBUG: MiniProfiler#delete."<<std::endl;
}
int MiniProfiler::number_of_registered_tasks() const {
return tasks.size();
}
void MiniProfiler::register_task(const std::string& t_name) {
struct MiniProfilerTask* new_task = new struct MiniProfilerTask;
new_task->name = t_name;
new_task->elapsed_clock_ticks = 0;
new_task->currently_active = false;
tasks.push_back( *new_task );
}
int MiniProfiler::search(const std::string& t_name) const {
// This should really be implemented using a hash table, but as long as we one
// use very few tasks this should work
for(int i = 0; i < tasks.size(); i++) {
if( tasks[i].name == t_name ) {
// std::cout <<"DEBUG: found the task."<<std::endl;
return i;
}
}
return -1;
}
float MiniProfiler::get_current_time(int task_index) const {
if( tasks[task_index].currently_active ) {
clock_t now = clock();
return float(tasks[task_index].elapsed_clock_ticks + \
(now - tasks[task_index].clock_tick_of_activation))/CLOCKS_PER_SEC;
}
// or, if the task is inactive then all previous intervals have already been added up
return float(tasks[task_index].elapsed_clock_ticks)/CLOCKS_PER_SEC;
}
void MiniProfiler::resuming_task(const std::string& t_name) {
int t_index = search(t_name);
if( t_index == -1 ) {
std::cout <<"Error in MiniProfiler#resuming_task: Invalid task key."<<std::endl;
exit(1);
}
if( tasks[t_index].currently_active ) {
std::cout <<"Warning in MiniProfiler#resuming_task: task '"<<t_name<<"' is already running!"<<std::endl;
} else {
tasks[t_index].clock_tick_of_activation = clock();
tasks[t_index].currently_active = true;
}
}
void MiniProfiler::stopping_task(const std::string& t_name) {
int t_index = search(t_name);
if( t_index == -1 ) {
std::cout <<"Error in MiniProfiler#stopping_task: Invalid task key."<<std::endl;
exit(1);
}
if( !tasks[t_index].currently_active ) {
std::cout <<"Warning in MiniProfiler#stopping_task: task '"<<t_name<<"' is not running!"<<std::endl;
} else {
tasks[t_index].elapsed_clock_ticks += clock() - tasks[t_index].clock_tick_of_activation;
tasks[t_index].currently_active = false;
}
}
float MiniProfiler::get_current_time(const std::string& t_name) const {
int task_index = search(t_name);
if( task_index >= 0 ) { // if such a task was found
return get_current_time(task_index);
}
return 0.0;
}
std::string MiniProfiler::summary() {
clock_t now = clock();
std::stringstream summ;
for(int i = 0; i < tasks.size(); i++) {
summ <<"- "<<tasks[i].name<<": "<<get_current_time(i)<<"s\n";
}
return summ.str();
}