Complete Note#1 in the http://wiki.osgeo.org/wiki/GEOS_Provenance_Review to get out...
[geos.git] / src / util / Profiler.cpp
blobad019792de829717d0cb1506c9f5107fc323ea8a
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 #include <geos/profiler.h>
16 #include <iostream>
17 #include <map>
18 #include <string>
19 #include <utility>
21 using namespace std;
23 namespace geos {
24 namespace util { // geos.util
26 Profile::Profile(string newname)
28 name = newname;
29 totaltime = 0;
30 min = max = avg = 0;
33 Profile::~Profile()
37 #if 0
38 void
39 Profile::start()
41 gettimeofday(&starttime, NULL);
44 void
45 Profile::stop()
47 gettimeofday(&stoptime, NULL);
48 double elapsed = 1000000*(stoptime.tv_sec-starttime.tv_sec)+
49 (stoptime.tv_usec-starttime.tv_usec);
51 timings.push_back(elapsed);
52 totaltime += elapsed;
53 if ( timings.size() == 1 ) max = min = elapsed;
54 else
56 if ( elapsed > max ) max = elapsed;
57 if ( elapsed < min ) min = elapsed;
59 avg = totaltime / timings.size();
61 #endif
63 double
64 Profile::getMax() const
66 return max;
69 double
70 Profile::getMin() const
72 return min;
75 double
76 Profile::getAvg() const
78 return avg;
81 double
82 Profile::getTot() const
84 return totaltime;
87 size_t
88 Profile::getNumTimings() const
90 return timings.size();
93 Profiler::Profiler()
97 Profiler::~Profiler()
99 map<string, Profile *>::const_iterator it;
100 for ( it=profs.begin(); it != profs.end(); ++it )
102 delete it->second;
106 void
107 Profiler::start(string name)
109 Profile *prof = get(name);
110 prof->start();
113 void
114 Profiler::stop(string name)
116 map<string, Profile *>::iterator iter = profs.find(name);
117 if ( iter == profs.end() ) {
118 cerr<<name<<": no such Profile started";
119 return;
121 iter->second->stop();
124 Profile *
125 Profiler::get(string name)
127 Profile *prof;
128 map<string, Profile *>::iterator iter = profs.find(name);
129 if ( iter == profs.end() ) {
130 prof = new Profile(name);
131 profs.insert(pair<string, Profile *>(name, prof));
132 } else {
133 prof = iter->second;
135 return prof;
138 Profiler *
139 Profiler::instance()
141 static Profiler internal_profiler;
142 return &internal_profiler;
146 ostream&
147 operator<< (ostream &os, const Profile &prof)
149 os << " num:"<<prof.getNumTimings()<<" min:"<<
150 prof.getMin()<<" max:"<<prof.getMax()<<
151 " avg:"<<prof.getAvg()<<" tot:"<<prof.getTot()<<
152 " ["<<prof.name<<"]";
153 return os;
156 ostream&
157 operator<< (ostream &os, const Profiler &prof)
159 map<string, Profile *>::const_iterator it;
160 for ( it=prof.profs.begin(); it != prof.profs.end(); ++it )
162 os<<*(it->second)<<endl;
164 return os;
168 } // namespace geos.util
169 } // namespace geos
171 /**********************************************************************
172 * $Log$
173 * Revision 1.10 2006/06/12 11:29:24 strk
174 * unsigned int => size_t
176 * Revision 1.9 2006/03/07 14:18:34 strk
177 * Profiler singleton implemented with a function-static Profiler instance
179 * Revision 1.8 2006/03/06 19:40:48 strk
180 * geos::util namespace. New GeometryCollection::iterator interface, many cleanups.
182 * Revision 1.7 2006/03/03 10:46:22 strk
183 * Removed 'using namespace' from headers, added missing headers in .cpp files, removed useless includes in headers (bug#46)
185 * Revision 1.6 2005/02/01 14:18:04 strk
186 * Made profiler start/stop inline
188 * Revision 1.5 2005/02/01 13:44:59 strk
189 * More profiling labels.
191 * Revision 1.4 2004/12/03 22:52:56 strk
192 * enforced const return of CoordinateSequence::toVector() method to derivate classes.
194 * Revision 1.3 2004/11/08 12:15:35 strk
195 * Added number of gathered timings in output.
197 * Revision 1.2 2004/11/08 11:19:39 strk
198 * Profiler::get() always return a Profile (new if not existant).
200 * Revision 1.1 2004/11/01 16:43:04 strk
201 * Added Profiler code.
202 * Temporarly patched a bug in DoubleBits (must check drawbacks).
203 * Various cleanups and speedups.
205 **********************************************************************/