This commit is contained in:
Paolo Cignoni 2011-12-14 15:03:10 +00:00
parent 412ef1aa65
commit b4f463e909
4 changed files with 144 additions and 366 deletions

View File

@ -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

View File

@ -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 <windows.h>
#else
#include <sys/time.h>
#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

View File

@ -1,77 +0,0 @@
#include "L3DTimer.h"
#ifdef DEVICE_X86_WIN
#include <Windows.h>
#else
#include <sys/time.h>
#endif
#include <time.h>
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
//---------------------------------------------------------------------------

View File

@ -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 _CHRONOMETER_H_
#define _CHRONOMETER_H_
#ifndef _L3D_TIMER_
#define _L3D_TIMER_
#ifdef QT_CORE_LIB
#include "../../L3DPlatform.h"
#include <QTime>
class L3DTimer
{
namespace mt{
typedef QTime Clock;
}//namespace
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
};
#else
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#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_