Complete Note#1 in the http://wiki.osgeo.org/wiki/GEOS_Provenance_Review to get out...
[geos.git] / include / geos / profiler.h
blobf3e7abec5015b6fecf251ece62a55640f9369fe9
1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2001-2002 Vivid Solutions Inc.
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
13 **********************************************************************/
15 #ifndef GEOS_PROFILER_H
16 #define GEOS_PROFILER_H
18 #include <geos/export.h>
20 /* For MingW builds with __STRICT_ANSI__ (-ansi) */
21 #if defined(__MINGW32__)
22 /* Allow us to check for presence of gettimeofday in MingW */
23 #include <config.h>
25 #include <sys/time.h>
26 extern "C" {
27 extern _CRTIMP void __cdecl _tzset (void);
28 __MINGW_IMPORT int _daylight;
29 __MINGW_IMPORT long _timezone;
30 __MINGW_IMPORT char *_tzname[2];
32 #endif
34 #if defined(_MSC_VER) || defined(__MINGW32__) && !defined(HAVE_GETTIMEOFDAY)
35 #include <geos/timeval.h>
36 #else
37 #include <sys/time.h>
38 #endif
40 #include <map>
41 #include <memory>
42 #include <iostream>
43 #include <string>
44 #include <vector>
46 #ifndef PROFILE
47 #define PROFILE 0
48 #endif
50 #ifdef _MSC_VER
51 #pragma warning(push)
52 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
53 #endif
55 namespace geos {
56 namespace util {
60 * \class Profile utils.h geos.h
62 * \brief Profile statistics
64 class GEOS_DLL Profile {
65 public:
66 /** \brief Create a named profile */
67 Profile(std::string name);
69 /** \brief Destructor */
70 ~Profile();
72 /** \brief start a new timer */
73 void start() {
74 gettimeofday(&starttime, NULL);
77 /** \brief stop current timer */
78 void stop()
80 gettimeofday(&stoptime, NULL);
81 double elapsed = 1000000*(stoptime.tv_sec-starttime.tv_sec)+
82 (stoptime.tv_usec-starttime.tv_usec);
84 timings.push_back(elapsed);
85 totaltime += elapsed;
86 if ( timings.size() == 1 ) max = min = elapsed;
87 else
89 if ( elapsed > max ) max = elapsed;
90 if ( elapsed < min ) min = elapsed;
92 avg = totaltime / timings.size();
95 /** \brief Return Max stored timing */
96 double getMax() const;
98 /** \brief Return Min stored timing */
99 double getMin() const;
101 /** \brief Return total timing */
102 double getTot() const;
104 /** \brief Return average timing */
105 double getAvg() const;
107 /** \brief Return number of timings */
108 size_t getNumTimings() const;
110 /** \brief Profile name */
111 std::string name;
114 private:
116 /* \brief current start and stop times */
117 struct timeval starttime, stoptime;
119 /* \brief actual times */
120 std::vector<double> timings;
122 /* \brief total time */
123 double totaltime;
125 /* \brief max time */
126 double max;
128 /* \brief max time */
129 double min;
131 /* \brief max time */
132 double avg;
137 * \class Profiler utils.h geos.h
139 * \brief Profiling class
142 class GEOS_DLL Profiler {
144 public:
146 Profiler();
147 ~Profiler();
150 * \brief
151 * Return the singleton instance of the
152 * profiler.
154 static Profiler *instance(void);
157 * \brief
158 * Start timer for named task. The task is
159 * created if does not exist.
161 void start(std::string name);
164 * \brief
165 * Stop timer for named task.
166 * Elapsed time is registered in the given task.
168 void stop(std::string name);
170 /** \brief get Profile of named task */
171 Profile *get(std::string name);
173 std::map<std::string, Profile *> profs;
177 /** \brief Return a string representing the Profile */
178 std::ostream& operator<< (std::ostream& os, const Profile&);
180 /** \brief Return a string representing the Profiler */
181 std::ostream& operator<< (std::ostream& os, const Profiler&);
183 } // namespace geos::util
184 } // namespace geos
186 #ifdef _MSC_VER
187 #pragma warning(pop)
188 #endif
190 #endif // ndef GEOS_PROFILER_H
192 /**********************************************************************
193 * $Log$
194 * Revision 1.8 2006/06/12 11:29:23 strk
195 * unsigned int => size_t
197 * Revision 1.7 2006/03/06 19:40:47 strk
198 * geos::util namespace. New GeometryCollection::iterator interface, many cleanups.
200 * Revision 1.6 2006/03/03 10:46:21 strk
201 * Removed 'using namespace' from headers, added missing headers in .cpp files, removed useless includes in headers (bug#46)
203 * Revision 1.5 2005/02/01 14:18:04 strk
204 * Made profiler start/stop inline
206 * Revision 1.4 2004/12/03 16:21:07 frank
207 * dont try for sys/time.h with MSVC
209 * Revision 1.3 2004/11/30 16:44:16 strk
210 * Added gettimeofday implementation for win32, curtesy of Wu Yongwei.
212 * Revision 1.2 2004/11/04 08:49:13 strk
213 * Unlinked new documentation.
215 * Revision 1.1 2004/11/01 16:43:04 strk
216 * Added Profiler code.
217 * Temporarly patched a bug in DoubleBits (must check drawbacks).
218 * Various cleanups and speedups.
220 **********************************************************************/