Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnCodeProfiler.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnCodeProfiler.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <ctime>
16 #include <vector>
17 #include <map>
18 #include <string>
19 #include <iostream>
20 
21 // macro for function tic/toc
22 #define DRWN_FCN_TIC {drwnCodeProfiler::tic(drwnCodeProfiler::getHandle(__PRETTY_FUNCTION__));}
23 #define DRWN_FCN_TOC {drwnCodeProfiler::toc(drwnCodeProfiler::getHandle(__PRETTY_FUNCTION__));}
24 
69 public:
70  static bool enabled;
71 
72 private:
73  //@cond
74  class drwnCodeProfilerEntry {
75  public:
76  clock_t startClock;
77  time_t startTime;
78  unsigned long totalClock;
79  double totalTime;
80  int totalCalls;
81 
82  drwnCodeProfilerEntry() { clear(); };
83  ~drwnCodeProfilerEntry() { /* do nothing */ };
84 
85  inline void clear() {
86  startClock = clock();
87  totalClock = 0L;
88 
89  startTime = std::time(NULL);
90  totalTime = 0L;
91 
92  totalCalls = 0;
93  }
94  inline void tic() {
95  startClock = clock();
96  startTime = std::time(NULL);
97  }
98  inline void toc() {
99  clock_t endClock = clock();
100  time_t endTime;
101  endTime = std::time(NULL);
102 
103  if (endClock >= startClock) {
104  totalClock += (endClock - startClock);
105  } else {
106  totalClock += ((clock_t)(-1) - startClock) + endClock;
107  }
108  startClock = endClock;
109 
110  totalTime += difftime(endTime, startTime);
111 
112  totalCalls += 1;
113  }
114  };
115  //@endcond
116 
117  static std::vector<drwnCodeProfilerEntry> _entries;
118  static std::map<std::string, int> _names;
119 
120 public:
121  drwnCodeProfiler() { /* do nothing */ }
122  ~drwnCodeProfiler() { /* do nothing */ }
123 
125  static int getHandle(const char *name);
126 
128  static inline void clear(int handle) {
129  if (enabled) _entries[handle].clear();
130  }
132  static inline void tic(int handle) {
133  if (enabled) _entries[handle].tic();
134  }
136  static inline void toc(int handle) {
137  if (enabled) _entries[handle].toc();
138  }
140  static inline double walltime(int handle) {
141  if (!enabled) return -1.0;
142  return _entries[handle].totalTime;
143  }
145  static inline double time(int handle) {
146  if (!enabled) return -1.0;
147  return (double)_entries[handle].totalClock / (double)CLOCKS_PER_SEC;
148  }
150  static inline int calls(int handle) {
151  if (!enabled) return -1;
152  return _entries[handle].totalCalls;
153  }
154 
156  static void print();
157 };
158 
static int calls(int handle)
return number of times handle has been profiled
Definition: drwnCodeProfiler.h:150
static void tic(int handle)
starting timing execution of handle
Definition: drwnCodeProfiler.h:132
static double time(int handle)
return total CPU running time of handle (in seconds)
Definition: drwnCodeProfiler.h:145
static void toc(int handle)
stop timing execution of handle (and update number of calls)
Definition: drwnCodeProfiler.h:136
static double walltime(int handle)
return total real-world running time of handle (in seconds)
Definition: drwnCodeProfiler.h:140
static int getHandle(const char *name)
return a handle for profiling a code block called name
Definition: drwnCodeProfiler.cpp:34
static bool enabled
set true to enable profiling
Definition: drwnCodeProfiler.h:70
Static class for providing profile information on functions.
Definition: drwnCodeProfiler.h:68
static void print()
display profile information for all handles (to message logger)
Definition: drwnCodeProfiler.cpp:52
static void clear(int handle)
clear all profiling information for handle
Definition: drwnCodeProfiler.h:128