91 dsum(0.0), dsqrsum(0.0), dmax(0.0), dmin(0.0),
92 idx_timebuf(-1), is_recording(false) {}
96 start_count = tickCount();
99 void start(
double start_count_value) {
100 start_count = start_count_value;
104 double stop_count = tickCount();
105 double delta = (stop_count - start_count) * tickPeriod();
112 dsqrsum += delta * delta;
123 if (idx_timebuf >= 0) {
124 timebuf.at(idx_timebuf) = delta;
126 if (idx_timebuf >= (
int)
timebuf.size()) {
131 is_recording =
false;
138 if (
nsample == 0 || fn == NULL) {
139 std::string empty_str;
151 rec.
stddev = var < 0 ? 0.0 : sqrt(var);
155 if (idx_timebuf >= 0) {
156 int bufsize = (int)
timebuf.size();
157 if (
timebuf.at(idx_timebuf) < 0) {
158 rec.
timebuf.assign(idx_timebuf, 0);
159 for (
int i = 0; i < idx_timebuf; i++) {
163 rec.
timebuf.assign(bufsize, 0);
164 for (
int i = idx_timebuf; i < bufsize; i++) {
167 for (
int i = 0; i < idx_timebuf; i++) {
186 inline static double tickCount();
187 inline static double tickPeriod();
210 template<
typename TimerType>
220 const std::string& outfile =
"")
221 : ntimer(n), timer(ntimer), outfile(outfile) {
229 const std::string& outfile =
"")
230 : ntimer(ntimer_default), timer(ntimer), outfile(outfile) {
236 std::cout << report();
238 std::ofstream ofs(outfile);
250 if (i >= ntimer) { expand(i + 1); }
261 int i = findLabel(label);
275 if (i >= ntimer) { expand(i + 1); }
287 int i = findLabel(label);
300 if (i >= ntimer) { expand(i + 1); }
301 double t = timer.at(i).stop();
302 timer.at(i).start(t);
312 int i = findLabel(label);
313 double t = timer.at(i).stop();
314 timer.at(i).start(t);
333 if (i >= ntimer) { expand(i + 1); }
334 timer.at(i).initTimeBuf(len);
346 int i = findLabel(label);
347 timer.at(i).initTimeBuf(len);
361 std::string label_str(label);
362 if (labelExists(label_str)) {
363 int old_i = label_assoc[label_str];
368 }
else if (hasLabel(i)) {
371 doSetLabel(i, label_str);
383 if (i >= ntimer) { expand(i + 1); }
384 timer.at(i).setReporterFunc(fn);
396 int i = findLabel(label);
397 timer.at(i).setReporterFunc(fn);
408 common_reporter_func = fn;
409 for (
int i = 0; i < ntimer; i++) {
410 timer.at(i).setReporterFunc(fn);
425 if (i >= ntimer) { expand(i + 1); }
426 return timer.at(i).report(fn);
438 int i = findLabel(label);
439 return timer.at(i).report(fn);
451 std::ostringstream stream;
452 for (
int i = 0; i < (int)timer.size(); i++) {
453 stream << timer.at(i).report(fn);
469 if (i >= ntimer) { expand(i + 1); }
471 timer.at(i).calcStat(rec);
482 int i = findLabel(label);
484 timer.at(i).calcStat(rec);
489 std::ostringstream stream;
490 if (!rec.
label.empty()) {
491 stream << rec.
label <<
":" << std::endl;
493 stream << rec.
id <<
":" << std::endl;
495 stream << std::fixed << std::setprecision(3)
496 <<
" mean = " << 1000.0 * rec.
mean 497 <<
" [ms], std = " << 1000.0 * rec.
stddev 498 <<
", max/min = " << 1000.0 * rec.
maximum 501 <<
" iter." << std::endl;
506 std::ostringstream stream;
507 stream << rec.
id <<
"\t" 508 <<
"\"" << rec.
label <<
"\"\t" 509 << std::fixed << std::setprecision(3)
510 << 1000.0 * rec.
mean <<
"\t" 511 << 1000.0 * rec.
stddev <<
"\t" 512 << 1000.0 * rec.
maximum <<
"\t" 513 << 1000.0 * rec.
minimum <<
"\t" 520 common_reporter_func = rfunc;
521 for (
int i = 0; i < ntimer; i++) {
522 timer.at(i).setId(i);
523 timer.at(i).setReporterFunc(rfunc);
526 inline void clearLabel(
int i) {
527 label_assoc.erase(timer.at(i).label);
528 timer.at(i).setLabel(
"");
530 inline void doSetLabel(
int i, std::string&
label) {
531 timer.at(i).setLabel(label);
532 label_assoc[
label] = i;
534 inline void expand(
int new_ntimer) {
535 timer.resize(new_ntimer, TimerType());
536 for (
int i = ntimer; i < new_ntimer; i++) {
537 timer.at(i).setId(i);
538 timer.at(i).setReporterFunc(common_reporter_func);
542 inline int findLabel(
const char *label) {
543 std::string label_str(label);
544 std::map<std::string, int>::iterator it = label_assoc.find(label_str);
545 if (it == label_assoc.end()) {
547 doSetLabel(ntimer - 1, label_str);
549 return label_assoc[label_str];
551 bool labelExists(
const std::string& label)
const {
552 return label_assoc.find(label) != label_assoc.end();
554 bool hasLabel(
int i)
const {
555 return label_assoc.find(timer.at(i).label) != label_assoc.end();
558 static const int ntimer_default = 20;
560 std::vector<TimerType> timer;
561 std::map<std::string, int> label_assoc;
576 template <
typename TimerType>
597 idx = st.start(label);
613 #if defined WIN32 || defined _WIN32 || defined WINCE 616 LARGE_INTEGER counter;
617 QueryPerformanceCounter(&counter);
618 return (
double)counter.QuadPart;
621 static double tick_period = 0.0;
622 if (tick_period == 0.0) {
624 QueryPerformanceFrequency(&freq);
625 tick_period = 1.0 / (double)freq.QuadPart;
630 #elif (defined __linux || defined __linux__) && defined CLOCK_MONOTONIC_RAW 634 clock_gettime(CLOCK_MONOTONIC, &tp);
635 return (
double)((
long long int)tp.tv_sec * 1000000000 + tp.tv_nsec);
641 #elif defined __MACH__ && defined __APPLE__ 642 #include <mach/mach_time.h> 644 return (
double)mach_absolute_time();
647 static double tick_period = 0.0;
648 if (tick_period == 0.0) {
649 mach_timebase_info_data_t sTimebaseInfo;
650 mach_timebase_info(&sTimebaseInfo);
651 tick_period = sTimebaseInfo.numer / sTimebaseInfo.denom * 1e-9;
656 #else // other Unix-like 657 #include <sys/time.h> 661 gettimeofday(&tv, &tz);
662 return (
double)(tv.tv_sec * 1000000 + tv.tv_usec);
std::string report(const char *label, STimerReporterFunc fn=NULL)
STimerList_< STimer > STimerList
int start(const char *label)
STimerRecords calcStat(const char *label)
int stop(const char *label)
void initTimeBuf(int i, int len)
double stddev
standard deviation of elapsed time [s]
STimerList_(STimerReporterFunc const rfunc=reporterDefault, const std::string &outfile="")
std::string(* STimerReporterFunc)(STimerRecords &rec)
std::string label
Label of the timer.
std::string report(STimerReporterFunc fn=NULL) const
STimerList_(int n, const STimerReporterFunc rfunc=reporterDefault, const std::string &outfile="")
int id
Id number of the timer.
std::vector< double > timebuf
vector containing the recent values of elapsed time
static double tickCount()
void setReporterFunc(int i, STimerReporterFunc fn)
void start(double start_count_value)
void setLabel(int i, const char *label)
std::string report(STimerReporterFunc fn=NULL) const
double maximum
maximum of elapsed time [s]
void setReporterFunc(STimerReporterFunc fn)
int nsample
number of samples used to compute the above statistics
void registerTime(double delta)
STimerScoped_< STimer > STimerScoped
std::string report(int i, STimerReporterFunc fn=NULL)
void calcStat(STimerRecords &rec) const
STimerScoped_(STimerList_< TimerType > &stlist, int i)
int setReporterFunc(const char *label, STimerReporterFunc fn)
static double tickPeriod()
void setLabel(std::string l)
static std::string reporterTSV(STimerRecords &rec)
STimerScoped_(STimerList_< TimerType > &stlist, const char *label)
int laptime(const char *label)
double mean
mean of elapsed time [s]
void initTimeBuf(int len)
std::vector< double > timebuf
int initTimeBuf(const char *label, int len)
double minimum
minimum of elapsed time [s]
STimerReporterFunc reporter_func
STimerRecords calcStat(int i)
static std::string reporterDefault(STimerRecords &rec)
void setReporterFunc(STimerReporterFunc f)