forked from homenc/HElib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiming.cpp
More file actions
125 lines (107 loc) · 3.21 KB
/
timing.cpp
File metadata and controls
125 lines (107 loc) · 3.21 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Copyright (C) 2012,2013 IBM Corp.
* This program 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.
*
* This program 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 program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <ctime>
#include <iostream>
#include "timing.h"
#include <tr1/unordered_map>
#include <vector>
#include <algorithm>
#include <utility>
#include <cmath>
#include <cstring>
using namespace std;
//! A simple class to toggle timing information on and off
class FHEtimer {
public:
bool isOn; // a broken semaphore
clock_t counter;
long numCalls;
FHEtimer() { isOn=false; counter=0; numCalls=0; }
};
bool string_compare(const char *a, const char *b)
{
return strcmp(a, b) < 0;
}
bool FHEtimersOn=false;
typedef tr1::unordered_map<const char*,FHEtimer>timerMap;
static timerMap timers;
// Reset a timer for some label to zero
void resetFHEtimer(const char *fncName)
{
FHEtimer& t = timers[fncName]; // insert to map if not already there
t.numCalls = 0;
t.counter = 0;
if (t.isOn) t.counter -= std::clock();
}
// Start a timer
void startFHEtimer(const char *fncName)
{
FHEtimer& t = timers[fncName]; // insert to map if not already there
if (!t.isOn) {
t.isOn = true;
t.numCalls++;
t.counter -= std::clock();
}
}
// Stop a timer
void stopFHEtimer(const char *fncName)
{
FHEtimer& t = timers[fncName]; // insert to map if not already there
if (t.isOn) {
t.isOn = false;
t.counter += std::clock();
}
}
// Read the value of a timer (in seconds)
double getTime4func(const char *fncName) // returns time in seconds
{
FHEtimer& t = timers[fncName]; // insert to map if not already there
// If the counter is currently counting, add the clock() value
clock_t c = t.isOn? (t.counter + std::clock()) : t.counter;
return ((double)c)/CLOCKS_PER_SEC;
}
// Returns number of calls for that timer
long getNumCalls4func(const char *fncName)
{
FHEtimer& t = timers[fncName]; // insert to map if not already there
return t.numCalls;
}
void resetAllTimers()
{
for (timerMap::iterator it = timers.begin(); it != timers.end(); ++it)
resetFHEtimer(it->first);
}
// Print the value of all timers to stream
void printAllTimers(std::ostream& str)
{
vector<const char *> vec;
for (timerMap::iterator it = timers.begin(); it != timers.end(); ++it) {
vec.push_back(it->first);
}
sort(vec.begin(), vec.end(), string_compare);
for (vector<const char *>::iterator it = vec.begin(); it != vec.end(); ++it) {
double t = getTime4func(*it);
long n = getNumCalls4func(*it);
double ave;
if (n > 0) {
ave = t/n;
}
else {
ave = NAN;
}
str << " " << (*it) << ": " << t << " / " << n << " = " << ave << "\n";
}
}