Generated on Thu Apr 11 13:59:20 2019 for Gecode by doxygen 1.6.3

timer.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Mikael Lagerkvist, 2009
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #ifdef GECODE_USE_GETTIMEOFDAY
00035 #include <sys/time.h>
00036 #endif
00037 
00038 #ifdef GECODE_USE_CLOCK
00039 #include <ctime>
00040 #endif
00041 
00042 namespace Gecode { namespace Support {
00043 
00051   class GECODE_SUPPORT_EXPORT Timer {
00052   private:
00053 #if   defined(GECODE_USE_GETTIMEOFDAY)
00054     timeval t0; 
00055 #elif defined(GECODE_USE_CLOCK)
00056     clock_t t0; 
00057 #endif
00058   public:
00060     void start(void);
00062     double stop(void);
00063   };
00064 
00065   inline void
00066   Timer::start(void) {
00067 #if   defined(GECODE_USE_GETTIMEOFDAY)
00068     if (gettimeofday(&t0, NULL))
00069       throw OperatingSystemError("Timer::start[gettimeofday]");
00070 #elif defined(GECODE_USE_CLOCK)
00071     t0 = clock();
00072 #endif
00073   }
00074 
00075   inline double
00076   Timer::stop(void) {
00077 #if   defined(GECODE_USE_GETTIMEOFDAY)
00078     timeval t1, t;
00079     if (gettimeofday(&t1, NULL))
00080       throw OperatingSystemError("Timer::stop[gettimeofday]");
00081 
00082     // t = t1 - t2
00083     t.tv_sec = t1.tv_sec - t0.tv_sec;
00084     t.tv_usec = t1.tv_usec - t0.tv_usec;
00085     if (t.tv_usec < 0) {
00086       t.tv_sec--;
00087       t.tv_usec += 1000000;
00088     }
00089 
00090     return (static_cast<double>(t.tv_sec) * 1000.0) +
00091       (static_cast<double>(t.tv_usec)/1000.0);
00092 #elif defined(GECODE_USE_CLOCK)
00093     return (static_cast<double>(clock()-t0) / CLOCKS_PER_SEC) * 1000.0;
00094 #endif
00095   }
00096 
00097 }}
00098 
00099 // STATISTICS: support-any