Skip to content

Instantly share code, notes, and snippets.

@kunitoki
Created April 5, 2012 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kunitoki/2311551 to your computer and use it in GitHub Desktop.
Save kunitoki/2311551 to your computer and use it in GitHub Desktop.
boost::chrono in mapnik timer impl
diff --git a/include/mapnik/timer.hpp b/include/mapnik/timer.hpp
index 803f4d0..d7d7eb1 100644
--- a/include/mapnik/timer.hpp
+++ b/include/mapnik/timer.hpp
@@ -29,12 +29,49 @@
#include <sstream>
#include <iomanip>
#include <ctime>
-#ifndef WIN32
-#include <sys/time.h> // for gettimeofday() on unix
+
+#if BOOST_VERSION >= 104700
+ #include <boost/chrono.hpp>
+ using boost::chrono;
+#else
+ #ifndef WIN32
+ #include <sys/time.h> // for gettimeofday() on unix
+ #include <sys/resource.h>
+ #else
+ #include <windows.h>
+ #endif
#endif
+
namespace mapnik {
+#if BOOST_VERSION >= 104700
+ typedef chrono::system_clock::time_point time_point_t;
+#else
+ typedef double time_point_t;
+#endif
+
+
+inline time_point_t time_now()
+{
+#if BOOST_VERSION >= 104700
+ return chrono::system_clock::now();
+#else
+ #ifndef WIN32
+ struct timeval t;
+ struct timezone tzp;
+ gettimeofday(&t, &tzp);
+ return t.tv_sec + t.tv_usec * 1e-6;
+ #else
+ LARGE_INTEGER t, f;
+ QueryPerformanceCounter(&t);
+ QueryPerformanceFrequency(&f);
+ return double(t.QuadPart) / double(f.QuadPart);
+ #endif
+#endif
+}
+
+
// Measure times in both wall clock time and CPU times. Results are returned in milliseconds.
class timer
{
@@ -46,42 +83,48 @@ public:
void restart()
{
- _stopped = false;
- gettimeofday(&_wall_clock_start, NULL);
- _cpu_start = clock();
+ stopped_ = false;
+ wall_clock_start_ = time_now();
+ cpu_start_ = clock();
}
virtual void stop() const
{
- _stopped = true;
- _cpu_end = clock();
- gettimeofday(&_wall_clock_end, NULL);
+ stopped_ = true;
+ cpu_end_ = clock();
+ wall_clock_end_ = time_now();
}
double cpu_elapsed() const
{
// return elapsed CPU time in ms
- if (!_stopped)
+ if (! stopped_)
+ {
stop();
+ }
- return ((double) (_cpu_end - _cpu_start)) / CLOCKS_PER_SEC * 1000.0;
+ return ((double) (cpu_end_ - cpu_start_)) / CLOCKS_PER_SEC * 1000.0;
}
double wall_clock_elapsed() const
{
// return elapsed wall clock time in ms
- if (!_stopped)
+ if (! stopped_)
+ {
stop();
+ }
- long seconds = _wall_clock_end.tv_sec - _wall_clock_start.tv_sec;
- long useconds = _wall_clock_end.tv_usec - _wall_clock_start.tv_usec;
-
- return ((seconds) * 1000 + useconds / 1000.0) + 0.5;
+#if BOOST_VERSION >= 104700
+ chrono::duration<double, chrono::milliseconds> ms = wall_clock_end_ - wall_clock_start_;
+ return ms.count();
+#else
+ return (wall_clock_end_ - wall_clock_start_) * 1000.0;
+#endif
}
protected:
- mutable timeval _wall_clock_start, _wall_clock_end;
- mutable clock_t _cpu_start, _cpu_end;
- mutable bool _stopped;
+ mutable time_point_t wall_clock_start_, wall_clock_end_;
+ mutable clock_t cpu_start_, cpu_end_;
+ mutable bool stopped_;
};
// A progress_timer behaves like a timer except that the destructor displays
@springmeyer
Copy link

does not compile for me. there are a few _stopped which appear to still need renaming to stopped_ and it looks like chrono::milliseconds needs to be boost::milli?

@springmeyer
Copy link

I just forked https://gist.github.com/2311987 with new diff that compiles for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment