GEOS  3.3.3
profiler.h
1 /**********************************************************************
2  * $Id: profiler.h 3255 2011-03-01 17:56:10Z mloskot $
3  *
4  * GEOS - Geometry Engine Open Source
5  * http://geos.refractions.net
6  *
7  * Copyright (C) 2001-2002 Vivid Solutions Inc.
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU Lesser General Public Licence as published
11  * by the Free Software Foundation.
12  * See the COPYING file for more information.
13  *
14  **********************************************************************/
15 
16 #ifndef GEOS_PROFILER_H
17 #define GEOS_PROFILER_H
18 
19 #include <geos/export.h>
20 
21 /* For MingW builds with __STRICT_ANSI__ (-ansi) */
22 #if defined(__MINGW32__)
23 /* Allow us to check for presence of gettimeofday in MingW */
24 #include <config.h>
25 
26 #include <sys/time.h>
27 extern "C" {
28  extern _CRTIMP void __cdecl _tzset (void);
29  __MINGW_IMPORT int _daylight;
30  __MINGW_IMPORT long _timezone;
31  __MINGW_IMPORT char *_tzname[2];
32 }
33 #endif
34 
35 #if defined(_MSC_VER) || defined(__MINGW32__) && !defined(HAVE_GETTIMEOFDAY)
36 #include <geos/timeval.h>
37 #else
38 #include <sys/time.h>
39 #endif
40 
41 #include <map>
42 #include <memory>
43 #include <iostream>
44 #include <string>
45 #include <vector>
46 
47 #ifndef PROFILE
48 #define PROFILE 0
49 #endif
50 
51 #ifdef _MSC_VER
52 #pragma warning(push)
53 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
54 #endif
55 
56 namespace geos {
57 namespace util {
58 
59 
60 /*
61  * \class Profile utils.h geos.h
62  *
63  * \brief Profile statistics
64  */
65 class GEOS_DLL Profile {
66 public:
68  Profile(std::string name);
69 
71  ~Profile();
72 
74  void start() {
75  gettimeofday(&starttime, NULL);
76  }
77 
79  void stop()
80  {
81  gettimeofday(&stoptime, NULL);
82  double elapsed = 1000000*(stoptime.tv_sec-starttime.tv_sec)+
83  (stoptime.tv_usec-starttime.tv_usec);
84 
85  timings.push_back(elapsed);
86  totaltime += elapsed;
87  if ( timings.size() == 1 ) max = min = elapsed;
88  else
89  {
90  if ( elapsed > max ) max = elapsed;
91  if ( elapsed < min ) min = elapsed;
92  }
93  avg = totaltime / timings.size();
94  }
95 
97  double getMax() const;
98 
100  double getMin() const;
101 
103  double getTot() const;
104 
106  double getAvg() const;
107 
109  size_t getNumTimings() const;
110 
112  std::string name;
113 
114 
115 private:
116 
117  /* \brief current start and stop times */
118  struct timeval starttime, stoptime;
119 
120  /* \brief actual times */
121  std::vector<double> timings;
122 
123  /* \brief total time */
124  double totaltime;
125 
126  /* \brief max time */
127  double max;
128 
129  /* \brief max time */
130  double min;
131 
132  /* \brief max time */
133  double avg;
134 
135 };
136 
137 /*
138  * \class Profiler utils.h geos.h
139  *
140  * \brief Profiling class
141  *
142  */
143 class GEOS_DLL Profiler {
144 
145 public:
146 
147  Profiler();
148  ~Profiler();
149 
155  static Profiler *instance(void);
156 
162  void start(std::string name);
163 
169  void stop(std::string name);
170 
172  Profile *get(std::string name);
173 
174  std::map<std::string, Profile *> profs;
175 };
176 
177 
179 std::ostream& operator<< (std::ostream& os, const Profile&);
180 
182 std::ostream& operator<< (std::ostream& os, const Profiler&);
183 
184 } // namespace geos::util
185 } // namespace geos
186 
187 #ifdef _MSC_VER
188 #pragma warning(pop)
189 #endif
190 
191 #endif // ndef GEOS_PROFILER_H
192 
193 /**********************************************************************
194  * $Log$
195  * Revision 1.8 2006/06/12 11:29:23 strk
196  * unsigned int => size_t
197  *
198  * Revision 1.7 2006/03/06 19:40:47 strk
199  * geos::util namespace. New GeometryCollection::iterator interface, many cleanups.
200  *
201  * Revision 1.6 2006/03/03 10:46:21 strk
202  * Removed 'using namespace' from headers, added missing headers in .cpp files, removed useless includes in headers (bug#46)
203  *
204  * Revision 1.5 2005/02/01 14:18:04 strk
205  * Made profiler start/stop inline
206  *
207  * Revision 1.4 2004/12/03 16:21:07 frank
208  * dont try for sys/time.h with MSVC
209  *
210  * Revision 1.3 2004/11/30 16:44:16 strk
211  * Added gettimeofday implementation for win32, curtesy of Wu Yongwei.
212  *
213  * Revision 1.2 2004/11/04 08:49:13 strk
214  * Unlinked new documentation.
215  *
216  * Revision 1.1 2004/11/01 16:43:04 strk
217  * Added Profiler code.
218  * Temporarly patched a bug in DoubleBits (must check drawbacks).
219  * Various cleanups and speedups.
220  *
221  **********************************************************************/