00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef GEOS_PROFILER_H
00017 #define GEOS_PROFILER_H
00018
00019
00020 #if defined(__MINGW32__)
00021
00022 #include <config.h>
00023
00024 #include <sys/time.h>
00025 extern "C" {
00026 extern _CRTIMP void __cdecl _tzset (void);
00027 __MINGW_IMPORT int _daylight;
00028 __MINGW_IMPORT long _timezone;
00029 __MINGW_IMPORT char *_tzname[2];
00030 }
00031 #endif
00032
00033 #if defined(_MSC_VER) || defined(__MINGW32__) && !defined(HAVE_GETTIMEOFDAY)
00034 #include <geos/timeval.h>
00035 #else
00036 #include <sys/time.h>
00037 #endif
00038
00039 #include <map>
00040 #include <memory>
00041 #include <iostream>
00042 #include <string>
00043 #include <vector>
00044
00045 #ifndef PROFILE
00046 #define PROFILE 0
00047 #endif
00048
00049 namespace geos {
00050 namespace util {
00051
00052
00053
00054
00055
00056
00057
00058 class Profile {
00059 public:
00061 Profile(std::string name);
00062
00064 ~Profile();
00065
00067 void start() {
00068 gettimeofday(&starttime, NULL);
00069 }
00070
00072 void stop()
00073 {
00074 gettimeofday(&stoptime, NULL);
00075 double elapsed = 1000000*(stoptime.tv_sec-starttime.tv_sec)+
00076 (stoptime.tv_usec-starttime.tv_usec);
00077
00078 timings.push_back(elapsed);
00079 totaltime += elapsed;
00080 if ( timings.size() == 1 ) max = min = elapsed;
00081 else
00082 {
00083 if ( elapsed > max ) max = elapsed;
00084 if ( elapsed < min ) min = elapsed;
00085 }
00086 avg = totaltime / timings.size();
00087 }
00088
00090 double getMax() const;
00091
00093 double getMin() const;
00094
00096 double getTot() const;
00097
00099 double getAvg() const;
00100
00102 size_t getNumTimings() const;
00103
00105 std::string name;
00106
00107
00108 private:
00109
00110
00111 struct timeval starttime, stoptime;
00112
00113
00114 std::vector<double> timings;
00115
00116
00117 double totaltime;
00118
00119
00120 double max;
00121
00122
00123 double min;
00124
00125
00126 double avg;
00127
00128 };
00129
00130
00131
00132
00133
00134
00135
00136 class Profiler {
00137
00138 public:
00139
00140 Profiler();
00141 ~Profiler();
00142
00148 static Profiler *instance(void);
00149
00155 void start(std::string name);
00156
00162 void stop(std::string name);
00163
00165 Profile *get(std::string name);
00166
00167 std::map<std::string, Profile *> profs;
00168 };
00169
00170
00172 std::ostream& operator<< (std::ostream& os, const Profile&);
00173
00175 std::ostream& operator<< (std::ostream& os, const Profiler&);
00176
00177 }
00178 }
00179
00180 #endif // ndef GEOS_PROFILER_H
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210