00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #pragma once
00025 #ifndef __D_PROFILER__
00026 #define __D_PROFILER__
00027
00028 #include <map>
00029 #include <vector>
00030 #include <string>
00031
00032 #include "Timestamp.h"
00033
00034 namespace DUtils {
00035
00042 #define PROFILE_S(cmd, s) \
00043 { DUtils::Timestamp t_begin, t_end; \
00044 t_begin.setToCurrentTime(); \
00045 cmd; \
00046 t_end.setToCurrentTime(); \
00047 std::cout << s << " - elapsed time: " \
00048 << DUtils::Timestamp::Format(t_end - t_begin) \
00049 << std::endl; \
00050 }
00051
00056 #define PROFILE(cmd) PROFILE_S(cmd, "")
00057
00065 #define PROFILE_CODE(P, name, scale, cmd) \
00066 { (P).profile(name); \
00067 cmd; \
00068 (P).stopAndScale((scale), (name)); \
00069 }
00070
00072 class Profiler
00073 {
00074 public:
00075
00076
00077 static const float MS = 1e3;
00078 static const float SECONDS = 1;
00079
00080 public:
00081
00082 Profiler(const float scale = Profiler::SECONDS):
00083 m_last_profile(""), m_scale(scale){}
00084 virtual ~Profiler(){}
00085
00091 void profile(const std::string &name = "");
00092
00101 void stopAndScale(double scale, const std::string &name = "");
00102
00109 inline void stop(const std::string &name = "")
00110 {
00111 stopAndScale(m_scale, name);
00112 }
00113
00119 void add(double v, const std::string &name = "");
00120
00125 double back(const std::string &name = "") const;
00126
00131 void reset(const std::string &name = "");
00132
00136 inline void resetAll()
00137 {
00138 m_profiles.clear();
00139 }
00140
00145 void getEntryNames(std::vector<std::string> &names) const;
00146
00151 inline float getDefaultScale() const { return m_scale; }
00152
00157 inline void setDefaultScale(float scale) { m_scale = scale; }
00158
00163 double getMeanTime(const std::string &name = "") const ;
00164
00169 double getStdevTime(const std::string &name = "") const ;
00170
00175 double getMinTime(const std::string &name = "") const ;
00176
00181 double getMaxTime(const std::string &name = "") const ;
00182
00187 double getTotalTime(const std::string &name = "") const;
00188
00194 void getTime(std::vector<double> &time, const std::string &name = "") const;
00195
00204 void getStatistics(double &mean, double &stdev,
00205 double &min, double &max, const std::string &name = "") const;
00206
00214 void showStatistics(const std::string &name = "",
00215 const std::string &suffix = "s", double scale = 1.,
00216 ostream &out = std::cout) const;
00217
00218 protected:
00219
00221 std::map<std::string, std::vector<double> > m_profiles;
00222
00224 std::map<std::string, Timestamp> m_start_points;
00225
00227 std::string m_last_profile;
00228
00230 float m_scale;
00231
00232 };
00233
00234 }
00235
00236 #endif