this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Mikael Lagerkvist <lagerkvist@gecode.org> 5 * 6 * Copyright: 7 * Mikael Lagerkvist, 2009 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34#ifdef GECODE_USE_GETTIMEOFDAY 35#include <sys/time.h> 36#endif 37 38#ifdef GECODE_USE_CLOCK 39#include <ctime> 40#endif 41 42namespace Gecode { namespace Support { 43 44 /** \brief %Timer 45 * 46 * This class represents a best-effort at measuring wall-clock time 47 * in milliseconds. 48 * 49 * \ingroup FuncSupport 50 */ 51 class GECODE_SUPPORT_EXPORT Timer { 52 private: 53#if defined(GECODE_USE_GETTIMEOFDAY) 54 timeval t0; ///< Start time 55#elif defined(GECODE_USE_CLOCK) 56 clock_t t0; ///< Start time 57#endif 58 public: 59 /// Start timer 60 void start(void); 61 /// Get time since start of timer 62 double stop(void); 63 }; 64 65 inline void 66 Timer::start(void) { 67#if defined(GECODE_USE_GETTIMEOFDAY) 68 if (gettimeofday(&t0, nullptr)) 69 throw OperatingSystemError("Timer::start[gettimeofday]"); 70#elif defined(GECODE_USE_CLOCK) 71 t0 = clock(); 72#endif 73 } 74 75 inline double 76 Timer::stop(void) { 77#if defined(GECODE_USE_GETTIMEOFDAY) 78 timeval t1, t; 79 if (gettimeofday(&t1, nullptr)) 80 throw OperatingSystemError("Timer::stop[gettimeofday]"); 81 82 // t = t1 - t2 83 t.tv_sec = t1.tv_sec - t0.tv_sec; 84 t.tv_usec = t1.tv_usec - t0.tv_usec; 85 if (t.tv_usec < 0) { 86 t.tv_sec--; 87 t.tv_usec += 1000000; 88 } 89 90 return (static_cast<double>(t.tv_sec) * 1000.0) + 91 (static_cast<double>(t.tv_usec)/1000.0); 92#elif defined(GECODE_USE_CLOCK) 93 return (static_cast<double>(clock()-t0) / CLOCKS_PER_SEC) * 1000.0; 94#endif 95 } 96 97}} 98 99// STATISTICS: support-any