From b4f463e90933103f90f3fe3fa6217e70b8005263 Mon Sep 17 00:00:00 2001 From: cnr-isti-vclab Date: Wed, 14 Dec 2011 15:03:10 +0000 Subject: [PATCH] --- wrap/system/time/chronometer.cpp | 175 ----------------------------- wrap/system/time/chronometer.h | 76 ------------- wrap/system/time/clock.cpp | 77 ------------- wrap/system/time/clock.h | 182 ++++++++++++++++++++++++------- 4 files changed, 144 insertions(+), 366 deletions(-) delete mode 100644 wrap/system/time/chronometer.cpp delete mode 100644 wrap/system/time/chronometer.h delete mode 100644 wrap/system/time/clock.cpp diff --git a/wrap/system/time/chronometer.cpp b/wrap/system/time/chronometer.cpp deleted file mode 100644 index cd5209c6..00000000 --- a/wrap/system/time/chronometer.cpp +++ /dev/null @@ -1,175 +0,0 @@ - -#include "L3DChronometer.h" - - -/****************************************************************************** - -Implementacion de metodos multiplataforma - -*******************************************************************************/ - -// ----------------------------------------------------------------------------- - -void L3DChronometer::stop () -{ - if (state == TIMER_RUNNING || state == TIMER_PAUSED) { - if (state == TIMER_RUNNING) - updateTime (); - state = TIMER_STOPPED; - } -} - -// ----------------------------------------------------------------------------- - -void L3DChronometer::pause () -{ - if (state == TIMER_RUNNING) { - updateTime (); - state = TIMER_PAUSED; - } -} - -/****************************************************************************** - -Implementacion de metodos para Windows - -*******************************************************************************/ - -#ifdef DEVICE_X86_WIN - -// ----------------------------------------------------------------------------- - -L3DChronometer::L3DChronometer() { - tacum.QuadPart = 0; - QueryPerformanceFrequency(&freq); - state = TIMER_STOPPED; - } - -// ----------------------------------------------------------------------------- - -float L3DChronometer::timeSecs () { - return (float) tacum.QuadPart / freq.QuadPart; -} - -// ----------------------------------------------------------------------------- - -float L3DChronometer::timeMSecs () { - return (float) tacum.QuadPart / ((float)freq.QuadPart / 1000); -} - -// ----------------------------------------------------------------------------- - -float L3DChronometer::timeUSecs () { - return (float) tacum.QuadPart / ((float)freq.QuadPart / 1000000u); -} - -// ----------------------------------------------------------------------------- - -void L3DChronometer::updateTime () -{ - LARGE_INTEGER tnow; - - QueryPerformanceCounter(&tnow); - tacum.QuadPart += (tnow.QuadPart - tini.QuadPart); - tini.QuadPart = tnow.QuadPart; -} - -// ----------------------------------------------------------------------------- - -void L3DChronometer::start () -{ - if (state == TIMER_STOPPED) { - tacum.QuadPart = 0; - QueryPerformanceCounter(&tini); - state = TIMER_RUNNING; - } -} - -// ----------------------------------------------------------------------------- - -void L3DChronometer::resume () -{ - if (state == TIMER_PAUSED) { - QueryPerformanceCounter(&tini); - state = TIMER_RUNNING; - } -} - -// ----------------------------------------------------------------------------- - - - -/****************************************************************************** - -Implementacion de metodos para sistemas operativos posix - -*******************************************************************************/ - -#else - -// ----------------------------------------------------------------------------- - -L3DChronometer::L3DChronometer() { - sec = 0; - usec = 0; - state = TIMER_STOPPED; -} - -// ----------------------------------------------------------------------------- - -float L3DChronometer::timeSecs () { - return (float) sec + (float) usec / 1000000.0f; -} - -// ----------------------------------------------------------------------------- - -float L3DChronometer::timeMSecs () { - return (float) sec * 1000.0f + (float) usec / 1000.0f; -} - -// ----------------------------------------------------------------------------- - -float L3DChronometer::timeUSecs () { - return (float) sec * 1000000.0f + (float) usec; -} - -// ----------------------------------------------------------------------------- - -void L3DChronometer::updateTime () -{ - timeval tnow; - gettimeofday (&tnow, 0); - - sec += tnow.tv_sec - tini.tv_sec; - usec += tnow.tv_usec - tini.tv_usec; - if (usec < 0) { - sec --; - usec += 1000000u; - } -} - -// ----------------------------------------------------------------------------- - -void L3DChronometer::start () -{ - if (state == TIMER_STOPPED) { - sec = 0; usec = 0; - gettimeofday (&tini, 0); - state = TIMER_RUNNING; - } -} - -// ----------------------------------------------------------------------------- - -void L3DChronometer::resume () -{ - if (state == TIMER_PAUSED) { - gettimeofday (&tini, 0); - state = TIMER_RUNNING; - } -} - -// ----------------------------------------------------------------------------- - -#endif - diff --git a/wrap/system/time/chronometer.h b/wrap/system/time/chronometer.h deleted file mode 100644 index a5a6c5b9..00000000 --- a/wrap/system/time/chronometer.h +++ /dev/null @@ -1,76 +0,0 @@ -//******************************************************************* -// L3DChronometer.h -- Header for L3DChronometer.cpp -// Copyright (c) 2010 Jose Maria Noguera -// Aug 10, 2010 -// -// Jose Maria Noguera Rozua http://wwwdi.ujaen.es/~jnoguera/ -// -//******************************************************************* - - -#ifndef _L3D_CHRONOMETER_H_ -#define _L3D_CHRONOMETER_H_ - -#include "../../L3DPlatform.h" - -#ifdef DEVICE_X86_WIN - #include -#else - #include -#endif - - -#define TIMER_STOPPED 0 -#define TIMER_RUNNING 1 -#define TIMER_PAUSED 2 - -/** @brief Chronometer used to measure performance of algoritms */ - -class L3DChronometer{ - -public: - /** Initializes the Chronometer */ - L3DChronometer(); - /** Starts to measure time */ - void start (); - /** Pause without losing the measured time */ - void pause (); - /** Resume a previously paused Chronometer */ - void resume (); - /** Stops the Chronometer */ - void stop (); - float elapsed() { stop(); float r = timeMSecs(); resume(); return r } - - /** Gets time in seconds. The Chronometer must be stop */ - float timeSecs (); - /** Gets time in seconds. The Chronometer must be stop */ - float timeMSecs (); - /** Gets time in seconds. The Chronometer must be stop*/ - float timeUSecs (); - -private: - -#ifdef DEVICE_X86_WIN - /* Initial time */ - LARGE_INTEGER tini; - /* Seconds accumulator */ - LARGE_INTEGER tacum; - /* Frequency */ - LARGE_INTEGER freq; -#else - /** Initial time */ - timeval tini; - /* Seconds accumulator */ - unsigned sec; - /* MicroSeconds accumulator */ - long usec; -#endif - - /* Chronometer state: stopped, running, paused */ - int state; - /* Updates time accumulators */ - void updateTime (); - -}; - -#endif diff --git a/wrap/system/time/clock.cpp b/wrap/system/time/clock.cpp deleted file mode 100644 index 165b3758..00000000 --- a/wrap/system/time/clock.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "L3DTimer.h" - - -#ifdef DEVICE_X86_WIN - #include -#else - #include -#endif - - -#include - - - - -L3DTimer::L3DTimer() -{/* - base = 0; - initialized = false; - basetime = 0;*/ -} - -//--------------------------------------------------------------------------- - -int L3DTimer::GetCurrentSystemTime(void) -{ - int iReturnValue = -1; - - #ifdef DEVICE_X86_WIN - iReturnValue = Win_GetCurrentSystemTime(); - #else - iReturnValue = Linux_GetCurrentSystemTime(); - #endif - - return iReturnValue; -} - -//--------------------------------------------------------------------------- - -#ifdef DEVICE_X86_WIN - -int L3DTimer::Win_GetCurrentSystemTime(void) -{ - int curtime; - static int base; - static bool initialized = false; - if(!initialized) - { - base = timeGetTime() & 0xffff0000; - initialized = true; - } - curtime = timeGetTime() - base; - return curtime; -} - -//--------------------------------------------------------------------------- - -#else - -//--------------------------------------------------------------------------- - -int L3DTimer::Linux_GetCurrentSystemTime(void) -{ - struct timeval tp; - struct timezone tzp; - static int basetime; - gettimeofday(&tp, &tzp); - if(!basetime) - { - basetime = tp.tv_sec; - return tp.tv_usec / 1000; - } - return (tp.tv_sec - basetime) * 1000 + tp.tv_usec / 1000; -} -#endif - -//--------------------------------------------------------------------------- diff --git a/wrap/system/time/clock.h b/wrap/system/time/clock.h index 3fcbbf8d..c8d6a336 100644 --- a/wrap/system/time/clock.h +++ b/wrap/system/time/clock.h @@ -1,43 +1,149 @@ //******************************************************************* -// L3DTimer.h -- Header for L3DTimer.cpp -// Copyright (c) 2010 Jose Maria Noguera -// Aug 10, 2010 +// Clock.h -- Header for Clock.cpp +// Copyright (c) 2011 Jose Maria Noguera +// Dec 9, 2011 // // Jose Maria Noguera Rozua http://wwwdi.ujaen.es/~jnoguera/ // //******************************************************************* - - -#ifndef _L3D_TIMER_ -#define _L3D_TIMER_ - -#include "../../L3DPlatform.h" - -class L3DTimer -{ - -public: - L3DTimer(); - ~L3DTimer(){;} - - /** - This function retrieves the system time, in milliseconds. The system time is the time elapsed since the first call - to this function. - */ - static int GetCurrentSystemTime(void); - -private: -/* - static int base; - static bool initialized; - static int basetime; -*/ - #ifdef DEVICE_X86_WIN - static int Win_GetCurrentSystemTime(void); - #else - static int Linux_GetCurrentSystemTime(void); - #endif -}; - -#endif - + +#ifndef _CHRONOMETER_H_ +#define _CHRONOMETER_H_ + +#ifdef QT_CORE_LIB + +#include + +namespace mt{ + typedef QTime Clock; +}//namespace + +#else + +#ifdef _WIN32 + #include +#else + #include +#endif + +namespace mt{ + +/** @brief Clock used to measure performance of algoritms. +Behaves similarly to QTime (QT).*/ + +class Clock{ + +public: + /** Initializes the Clock */ + Clock() + { + #ifdef _WIN32 + QueryPerformanceFrequency(&freq); + #endif + } + + /** Returns the current time as reported by the system clock. */ + static Clock currentTime() + { + Clock c; + c.start(); + return c; + } + + /** Sets this time to the current time. This is practical for timing. */ + inline void start() + { + #ifdef _WIN32 + QueryPerformanceCounter(&tini); + #else + gettimeofday (&tini, 0); + #endif + } + + /** Sets this time to the current time and returns the number of + milliseconds that have elapsed since the last time start() + or restart() was called.*/ + inline int restart() + { + return elapsedAux(true); + } + + /** Returns the number of milliseconds that have elapsed since the + last time start() or restart() was called. */ + inline int elapsed() + { + return elapsedAux(false); + } + + + /* Returns the number of milliseconds from this time to t. If t is earlier + than this time, the number of milliseconds returned is negative.*/ + int msecsTo ( const Clock & t ) const + { + #ifdef _WIN32 + LARGE_INTEGER tacum; + tacum.QuadPart = (t.tini.QuadPart - this->tini.QuadPart); + return (int) tacum.QuadPart / ((int)freq.QuadPart / 1000); + #else + long int sec = t.tini.tv_sec - this->tini.tv_sec; + long int usec = t.tini.tv_usec - this->tini.tv_usec; + if (usec < 0) { + sec --; + usec += 1000000u; + } + return (int) sec * 1000.0f + (int) usec / 1000.0f; + #endif + } + +protected: + +#ifdef _WIN32 + /* Initial time */ + LARGE_INTEGER tini; + /* Frequency */ + LARGE_INTEGER freq; +#else + /** Initial time */ + timeval tini; +#endif + + /** Returns the number of milliseconds that have elapsed since the + last time start() or restart() was called. + If restart is true, it also sets this time to the current time.*/ + int elapsedAux( bool restart ) + { + #ifdef _WIN32 + LARGE_INTEGER tnow; + LARGE_INTEGER tacum; + + QueryPerformanceCounter(&tnow); + tacum.QuadPart = (tnow.QuadPart - tini.QuadPart); + + if(restart) + tini.QuadPart = tnow.QuadPart; + + return (int) tacum.QuadPart / ((int)freq.QuadPart / 1000); + + #else + timeval tnow; + gettimeofday (&tnow, 0); + + long int sec = tnow.tv_sec - tini.tv_sec; + long int usec = tnow.tv_usec - tini.tv_usec; + if (usec < 0) { + sec --; + usec += 1000000u; + } + if(restart) + tini = tnow; + + return (int) sec * 1000.0f + (int) usec / 1000.0f; + + #endif + } +}; + +}//namespace + +#endif //ifdef QT_CORE_LIB +#endif //ifndef _CHRONOMETER_H_