00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _TIMER_H
00021 #define _TIMER_H
00022
00023 #include <map>
00024 #include <list>
00025 #include <string.h>
00026
00027 #ifdef WIN32
00028 #include <windows.h>
00029 typedef LARGE_INTEGER time_type;
00030 #else
00031 #include <sys/time.h>
00032 typedef struct timeval time_type;
00033 #endif
00034
00035 time_type get_time();
00036 double time_to_msec(time_type &t);
00037 time_type operator-(const time_type &a, const time_type &b);
00038
00039 struct cmp_str
00040 {
00041 bool operator()(char const *a, char const *b)
00042 {
00043 return strcmp(a, b) < 0;
00044 }
00045 };
00046
00047
00048 class Timer {
00049 public:
00050 Timer() { start(); }
00051 void start();
00052 double stop();
00053 void resume();
00054 double value();
00055
00056 bool isrunning() const { return running; }
00057 private:
00058 bool running;
00059 double duration();
00060
00061
00062
00063 time_type s;
00064
00065
00066
00067 double total;
00068 };
00069
00070 class TaskTimer : public Timer {
00071 public:
00072
00073 TaskTimer(const char *name);
00074 TaskTimer *setTask(const char *task);
00075 double stop();
00076
00077 static void pushTask(const char *task);
00078 static void popTask();
00079 static void printStats();
00080
00081 protected:
00082 typedef std::list<TaskTimer> TaskList;
00083
00084 struct SelfIncl {
00085 double self, incl;
00086 SelfIncl() : self(0), incl(0) {}
00087 } ;
00088 typedef std::map<const char *, SelfIncl> CharDoubleMap;
00089
00090 void printStats(int indent, double total, CharDoubleMap &flatProfile);
00091
00092 const char *name;
00093
00094
00095 TaskList subtasks;
00096 TaskList::iterator currentTask;
00097
00098 #ifdef _OPENMP
00099 struct ThreadedTask;
00100 struct ThreadedTask {
00101 int nb_thread;
00102 double tot_time,self;
00103
00104 typedef std::map<const char *, ThreadedTask, cmp_str> SubTaskMap;
00105 SubTaskMap subtasks;
00106
00107 ThreadedTask() { nb_thread=0; tot_time=0; }
00108
00109 void print(const char *name, int indent, double total);
00110 void cmpFlatProfile(const char *name, CharDoubleMap &flatProfile);
00111 };
00112
00113 void addThreadTask(ThreadedTask &task);
00114 static double printThreadTreeInfo(CharDoubleMap &flatProfile);
00115 #endif
00116 };
00117
00118
00119
00120 #endif