GEOS  3.6.1
profiler.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2001-2002 Vivid Solutions Inc.
7  *
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.
12  *
13  **********************************************************************/
14 
15 #ifndef GEOS_PROFILER_H
16 #define GEOS_PROFILER_H
17 
18 #include <stdlib.h>
19 #include <geos/export.h>
20 
21 /* For MingW builds with __STRICT_ANSI__ (-ansi) */
23 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
24 /* Allow us to check for presence of gettimeofday in MingW */
25 #include <config.h>
26 
27 #include <sys/time.h>
28 extern "C" {
29  extern _CRTIMP void __cdecl _tzset (void);
30  __MINGW_IMPORT int _daylight;
31  __MINGW_IMPORT long _timezone;
32  __MINGW_IMPORT char *_tzname[2];
33 }
34 #endif
35 
36 #if defined(_MSC_VER) || defined(__MINGW32__) && !defined(HAVE_GETTIMEOFDAY) && !defined(__MINGW64_VERSION_MAJOR)
37 #include <geos/timeval.h>
38 #else
39 #include <sys/time.h>
40 #endif
41 
42 #include <map>
43 #include <memory>
44 #include <iostream>
45 #include <string>
46 #include <vector>
47 
48 #ifndef PROFILE
49 #define PROFILE 0
50 #endif
51 
52 #ifdef _MSC_VER
53 #pragma warning(push)
54 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
55 #endif
56 
57 namespace geos {
58 namespace util {
59 
60 
61 /*
62  * \class Profile utils.h geos.h
63  *
64  * \brief Profile statistics
65  */
66 class GEOS_DLL Profile {
67 public:
69  Profile(std::string name);
70 
72  ~Profile();
73 
75  void start() {
76  gettimeofday(&starttime, NULL);
77  }
78 
80  void stop()
81  {
82  gettimeofday(&stoptime, NULL);
83  double elapsed = 1000000*(stoptime.tv_sec-starttime.tv_sec)+
84  (stoptime.tv_usec-starttime.tv_usec);
85 
86  timings.push_back(elapsed);
87  totaltime += elapsed;
88  if ( timings.size() == 1 ) max = min = elapsed;
89  else
90  {
91  if ( elapsed > max ) max = elapsed;
92  if ( elapsed < min ) min = elapsed;
93  }
94  avg = totaltime / timings.size();
95  }
96 
98  double getMax() const;
99 
101  double getMin() const;
102 
104  double getTot() const;
105 
107  double getAvg() const;
108 
110  size_t getNumTimings() const;
111 
113  std::string name;
114 
115 
116 private:
117 
118  /* \brief current start and stop times */
119  struct timeval starttime, stoptime;
120 
121  /* \brief actual times */
122  std::vector<double> timings;
123 
124  /* \brief total time */
125  double totaltime;
126 
127  /* \brief max time */
128  double max;
129 
130  /* \brief max time */
131  double min;
132 
133  /* \brief max time */
134  double avg;
135 
136 };
137 
138 /*
139  * \class Profiler utils.h geos.h
140  *
141  * \brief Profiling class
142  *
143  */
144 class GEOS_DLL Profiler {
145 
146 public:
147 
148  Profiler();
149  ~Profiler();
150 
156  static Profiler *instance(void);
157 
163  void start(std::string name);
164 
170  void stop(std::string name);
171 
173  Profile *get(std::string name);
174 
175  std::map<std::string, Profile *> profs;
176 };
177 
178 
180 std::ostream& operator<< (std::ostream& os, const Profile&);
181 
183 std::ostream& operator<< (std::ostream& os, const Profiler&);
184 
185 } // namespace geos::util
186 } // namespace geos
187 
188 #ifdef _MSC_VER
189 #pragma warning(pop)
190 #endif
191 
192 #endif // ndef GEOS_PROFILER_H
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25