timer.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifdef GECODE_USE_GETTIMEOFDAY
00039 #include <sys/time.h>
00040 #endif
00041
00042 #ifdef GECODE_USE_CLOCK
00043 #include <ctime>
00044 #endif
00045
00046 namespace Gecode { namespace Support {
00047
00055 class GECODE_SUPPORT_EXPORT Timer {
00056 private:
00057 #if defined(GECODE_USE_GETTIMEOFDAY)
00058 timeval t0;
00059 #elif defined(GECODE_USE_CLOCK)
00060 clock_t t0;
00061 #endif
00062 public:
00064 void start(void);
00066 double stop(void);
00067 };
00068
00069 inline void
00070 Timer::start(void) {
00071 #if defined(GECODE_USE_GETTIMEOFDAY)
00072 if (gettimeofday(&t0, NULL))
00073 throw OperatingSystemError("Timer::start[gettimeofday]");
00074 #elif defined(GECODE_USE_CLOCK)
00075 t0 = clock();
00076 #endif
00077 }
00078
00079 inline double
00080 Timer::stop(void) {
00081 #if defined(GECODE_USE_GETTIMEOFDAY)
00082 timeval t1, t;
00083 if (gettimeofday(&t1, NULL))
00084 throw OperatingSystemError("Timer::stop[gettimeofday]");
00085
00086
00087 t.tv_sec = t1.tv_sec - t0.tv_sec;
00088 t.tv_usec = t1.tv_usec - t0.tv_usec;
00089 if (t.tv_usec < 0) {
00090 t.tv_sec--;
00091 t.tv_usec += 1000000;
00092 }
00093
00094 return (static_cast<double>(t.tv_sec) * 1000.0) +
00095 (static_cast<double>(t.tv_usec)/1000.0);
00096 #elif defined(GECODE_USE_CLOCK)
00097 return (static_cast<double>(clock()-t0) / CLOCKS_PER_SEC) * 1000.0;
00098 #endif
00099 }
00100
00101 }}
00102
00103