|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "tstopwatch.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
#include <stdlib.h></stdlib.h>
|
|
Toshihiro Shimizu |
890ddd |
#else //_WIN32
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#if defined(__APPLE_CC__)
|
|
Toshihiro Shimizu |
890ddd |
#include <unistd.h></unistd.h>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
#include <sys unistd.h=""></sys>
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
#include <limits.h></limits.h>
|
|
Toshihiro Shimizu |
890ddd |
#include <sys times.h=""></sys>
|
|
Toshihiro Shimizu |
890ddd |
#include <sys types.h=""></sys>
|
|
Toshihiro Shimizu |
890ddd |
#include <time.h></time.h>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifndef STW_TICKS_PER_SECOND
|
|
Toshihiro Shimizu |
890ddd |
#ifndef WIN32
|
|
Toshihiro Shimizu |
890ddd |
extern "C" long sysconf(int);
|
|
Toshihiro Shimizu |
890ddd |
#define STW_TICKS_PER_SECOND sysconf(_SC_CLK_TCK)
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
#define STW_TICKS_PER_SECOND CLK_TCK
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#define MAXSWNAMELENGHT 40
|
|
Toshihiro Shimizu |
890ddd |
#define MAXSWTIMELENGHT 12
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TStopWatch TStopWatch::StopWatch[10];
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
enum TimerType { TTUUnknown,
|
|
Toshihiro Shimizu |
890ddd |
TTUHiRes,
|
|
Toshihiro Shimizu |
890ddd |
TTUTickCount };
|
|
Toshihiro Shimizu |
890ddd |
static void determineTimer();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
static TimerType timerToUse = TTUUnknown;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
static LARGE_INTEGER perfFreq; // ticks per second
|
|
Toshihiro Shimizu |
890ddd |
static int perfFreqAdjust = 0; // in case Freq is too big
|
|
Toshihiro Shimizu |
890ddd |
static int overheadTicks = 0; // overhead in calling timer
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
static TimerType timerToUse = TTUTickCount;
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
using namespace std;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TStopWatch::TStopWatch(string name)
|
|
Toshihiro Shimizu |
890ddd |
: m_name(name), m_active(false), m_isRunning(false)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (timerToUse == TTUUnknown)
|
|
Toshihiro Shimizu |
890ddd |
determineTimer();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_start = 0;
|
|
Toshihiro Shimizu |
890ddd |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
m_startUser.dwHighDateTime = m_startUser.dwLowDateTime = 0;
|
|
Toshihiro Shimizu |
890ddd |
m_startSystem.dwHighDateTime = m_startSystem.dwLowDateTime = 0;
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
m_startUser = 0;
|
|
Toshihiro Shimizu |
890ddd |
m_startSystem = 0;
|
|
Toshihiro Shimizu |
890ddd |
#endif //_WIN32
|
|
Toshihiro Shimizu |
890ddd |
m_tm = 0;
|
|
Toshihiro Shimizu |
890ddd |
m_tmUser = 0;
|
|
Toshihiro Shimizu |
890ddd |
m_tmSystem = 0;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TStopWatch::~TStopWatch()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_active = false;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void TStopWatch::setStartToCurrentTime()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
FILETIME creationTime, exitTime;
|
|
Toshihiro Shimizu |
890ddd |
BOOL ret = GetProcessTimes(GetCurrentProcess(), // specifies the process of interest
|
|
Toshihiro Shimizu |
890ddd |
&creationTime,
|
|
Toshihiro Shimizu |
890ddd |
&exitTime,
|
|
Toshihiro Shimizu |
890ddd |
&m_startSystem,
|
|
Toshihiro Shimizu |
890ddd |
&m_startUser);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (timerToUse == TTUTickCount) {
|
|
Toshihiro Shimizu |
890ddd |
m_start = GetTickCount();
|
|
Toshihiro Shimizu |
890ddd |
} else {
|
|
Toshihiro Shimizu |
890ddd |
QueryPerformanceCounter(&m_hrStart);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
struct tms clk;
|
|
Toshihiro Shimizu |
890ddd |
m_start = times(&clk);
|
|
Toshihiro Shimizu |
890ddd |
m_startUser = clk.tms_utime;
|
|
Toshihiro Shimizu |
890ddd |
m_startSystem = clk.tms_stime;
|
|
Toshihiro Shimizu |
890ddd |
#endif //_WIN32
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void TStopWatch::reset()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_tm = 0;
|
|
Toshihiro Shimizu |
890ddd |
m_tmUser = 0;
|
|
Toshihiro Shimizu |
890ddd |
m_tmSystem = 0;
|
|
Toshihiro Shimizu |
890ddd |
setStartToCurrentTime();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void TStopWatch::start(bool resetFlag)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (resetFlag)
|
|
Toshihiro Shimizu |
890ddd |
reset();
|
|
Toshihiro Shimizu |
890ddd |
if (m_isRunning)
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
m_active = true;
|
|
Toshihiro Shimizu |
890ddd |
m_isRunning = true;
|
|
Toshihiro Shimizu |
890ddd |
setStartToCurrentTime();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
inline __int64 FileTimeToInt64(LPFILETIME pFileTime)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
__int64 val;
|
|
Toshihiro Shimizu |
890ddd |
val = pFileTime->dwHighDateTime;
|
|
Toshihiro Shimizu |
890ddd |
val <<= 32;
|
|
Toshihiro Shimizu |
890ddd |
val |= pFileTime->dwLowDateTime;
|
|
Toshihiro Shimizu |
890ddd |
return val;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
#endif //_WIN32
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
// Aggiunge il tempo trascorso fra start(startUser, startSystem) e l'istante corrente
|
|
Toshihiro Shimizu |
890ddd |
// a tm(tmUser, tmSystem)
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
static void checkTime(START start, START_USER startUser, START_SYSTEM startSystem,
|
|
Toshihiro Shimizu |
890ddd |
TM_TOTAL &tm, TM_USER &tmUser, TM_SYSTEM &tmSystem)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(timerToUse == TTUTickCount);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
DWORD tm_stop;
|
|
Toshihiro Shimizu |
890ddd |
FILETIME creationTime, exitTime, stopSystem, stopUser;
|
|
Toshihiro Shimizu |
890ddd |
BOOL ret = GetProcessTimes(GetCurrentProcess(), // specifies the process of interest
|
|
Toshihiro Shimizu |
890ddd |
&creationTime,
|
|
Toshihiro Shimizu |
890ddd |
&exitTime,
|
|
Toshihiro Shimizu |
890ddd |
&stopSystem,
|
|
Toshihiro Shimizu |
890ddd |
&stopUser);
|
|
Toshihiro Shimizu |
890ddd |
tm_stop = GetTickCount();
|
|
Toshihiro Shimizu |
890ddd |
assert(tm_stop >= start);
|
|
Toshihiro Shimizu |
890ddd |
tm += tm_stop - start; //total elapsed time
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
tmUser += FileTimeToInt64(&stopUser) - FileTimeToInt64(&startUser); //user elapsed time
|
|
Toshihiro Shimizu |
890ddd |
tmSystem += FileTimeToInt64(&stopSystem) - FileTimeToInt64(&startSystem); //system elapsed time
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#else //WIN32
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
struct tms clk;
|
|
Toshihiro Shimizu |
890ddd |
clock_t tm_stop;
|
|
Toshihiro Shimizu |
890ddd |
tm_stop = times(&clk);
|
|
Toshihiro Shimizu |
890ddd |
assert(tm_stop >= start);
|
|
Toshihiro Shimizu |
890ddd |
tm += tm_stop - start;
|
|
Toshihiro Shimizu |
890ddd |
tmUser += clk.tms_utime - startUser;
|
|
Toshihiro Shimizu |
890ddd |
tmSystem += clk.tms_stime - startSystem;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#endif //WIN32
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
// come checkTime, ma usa i timer ad alta risoluzione
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
namespace
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void hrCheckTime(LARGE_INTEGER start, START_USER startUser, START_SYSTEM startSystem,
|
|
Toshihiro Shimizu |
890ddd |
TM_TOTAL &tm, TM_USER &tmUser, TM_SYSTEM &tmSystem)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(timerToUse != TTUTickCount);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
LARGE_INTEGER hrTm_stop;
|
|
Toshihiro Shimizu |
890ddd |
FILETIME creationTime, exitTime, stopSystem, stopUser;
|
|
Toshihiro Shimizu |
890ddd |
BOOL ret = GetProcessTimes(GetCurrentProcess(), // specifies the process of interest
|
|
Toshihiro Shimizu |
890ddd |
&creationTime,
|
|
Toshihiro Shimizu |
890ddd |
&exitTime,
|
|
Toshihiro Shimizu |
890ddd |
&stopSystem,
|
|
Toshihiro Shimizu |
890ddd |
&stopUser);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
QueryPerformanceCounter(&hrTm_stop);
|
|
Toshihiro Shimizu |
890ddd |
assert(hrTm_stop.HighPart > start.HighPart ||
|
|
Toshihiro Shimizu |
890ddd |
hrTm_stop.HighPart == start.HighPart && hrTm_stop.LowPart >= start.LowPart);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
LARGE_INTEGER Freq = perfFreq;
|
|
Toshihiro Shimizu |
890ddd |
int Oht = overheadTicks;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
LARGE_INTEGER dtime;
|
|
Toshihiro Shimizu |
890ddd |
//faccio "a mano" la differenza dtime = m_tStop - m_tStart
|
|
Toshihiro Shimizu |
890ddd |
dtime.HighPart = hrTm_stop.HighPart - start.HighPart;
|
|
Toshihiro Shimizu |
890ddd |
if (hrTm_stop.LowPart >= start.LowPart)
|
|
Toshihiro Shimizu |
890ddd |
dtime.LowPart = hrTm_stop.LowPart - start.LowPart;
|
|
Toshihiro Shimizu |
890ddd |
else {
|
|
Toshihiro Shimizu |
890ddd |
assert(dtime.HighPart > 0);
|
|
Toshihiro Shimizu |
890ddd |
dtime.HighPart--;
|
|
Toshihiro Shimizu |
890ddd |
dtime.LowPart = hrTm_stop.LowPart + ~start.LowPart + 1;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int shift = 0;
|
|
Toshihiro Shimizu |
890ddd |
if (Freq.HighPart > 0) {
|
|
Toshihiro Shimizu |
890ddd |
int h = Freq.HighPart;
|
|
Toshihiro Shimizu |
890ddd |
while (h > 0) {
|
|
Toshihiro Shimizu |
890ddd |
h >>= 1;
|
|
Toshihiro Shimizu |
890ddd |
shift++;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
if ((dtime.HighPart >> shift) > 0) {
|
|
Toshihiro Shimizu |
890ddd |
int h = dtime.HighPart >> shift;
|
|
Toshihiro Shimizu |
890ddd |
while (h > 0) {
|
|
Toshihiro Shimizu |
890ddd |
h >>= 1;
|
|
Toshihiro Shimizu |
890ddd |
shift++;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
if (shift > 0) {
|
|
Toshihiro Shimizu |
890ddd |
dtime.QuadPart = Int64ShrlMod32(dtime.QuadPart, shift);
|
|
Toshihiro Shimizu |
890ddd |
Freq.QuadPart = Int64ShrlMod32(Freq.QuadPart, shift);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
assert(Freq.HighPart == 0);
|
|
Toshihiro Shimizu |
890ddd |
assert(dtime.HighPart == 0);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double totalTime = 1000.0 * dtime.LowPart / Freq.LowPart;
|
|
Toshihiro Shimizu |
890ddd |
tm += troundp(totalTime);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
tmUser += FileTimeToInt64(&stopUser) - FileTimeToInt64(&startUser); //user elapsed time
|
|
Toshihiro Shimizu |
890ddd |
tmSystem += FileTimeToInt64(&stopSystem) - FileTimeToInt64(&startSystem); //system elapsed time
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
} //namespace
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#endif //WIN32
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void TStopWatch::stop()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (!m_isRunning)
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
m_isRunning = false;
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
if (timerToUse == TTUTickCount)
|
|
Toshihiro Shimizu |
890ddd |
checkTime(m_start, m_startUser, m_startSystem, m_tm, m_tmUser, m_tmSystem);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
hrCheckTime(m_hrStart, m_startUser, m_startSystem, m_tm, m_tmUser, m_tmSystem);
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
checkTime(m_start, m_startUser, m_startSystem, m_tm, m_tmUser, m_tmSystem);
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void TStopWatch::getElapsedTime(TM_TOTAL &tm, TM_USER &user, TM_SYSTEM &system)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (m_isRunning) {
|
|
Toshihiro Shimizu |
890ddd |
TM_TOTAL cur_tm = 0;
|
|
Toshihiro Shimizu |
890ddd |
TM_USER cur_tmUser = 0;
|
|
Toshihiro Shimizu |
890ddd |
TM_SYSTEM cur_tmSystem = 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
if (timerToUse == TTUTickCount)
|
|
Toshihiro Shimizu |
890ddd |
checkTime(m_start, m_startUser, m_startSystem, cur_tm, cur_tmUser, cur_tmSystem);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
hrCheckTime(m_hrStart, m_startUser, m_startSystem, cur_tm, cur_tmUser, cur_tmSystem);
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
checkTime(m_start, m_startUser, m_startSystem, cur_tm, cur_tmUser, cur_tmSystem);
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
tm = m_tm + cur_tm;
|
|
Toshihiro Shimizu |
890ddd |
user = m_tmUser + cur_tmUser;
|
|
Toshihiro Shimizu |
890ddd |
system = m_tmSystem + cur_tmSystem;
|
|
Toshihiro Shimizu |
890ddd |
} else {
|
|
Toshihiro Shimizu |
890ddd |
tm = m_tm;
|
|
Toshihiro Shimizu |
890ddd |
user = m_tmUser;
|
|
Toshihiro Shimizu |
890ddd |
system = m_tmSystem;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TUINT32 TStopWatch::getTotalTime()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TM_TOTAL tm;
|
|
Toshihiro Shimizu |
890ddd |
TM_USER user;
|
|
Toshihiro Shimizu |
890ddd |
TM_SYSTEM system;
|
|
Toshihiro Shimizu |
890ddd |
getElapsedTime(tm, user, system);
|
|
Toshihiro Shimizu |
890ddd |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
return tm;
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
return (TINT32)(tm * 1000) / STW_TICKS_PER_SECOND;
|
|
Toshihiro Shimizu |
890ddd |
#endif //_WIN32
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TUINT32 TStopWatch::getUserTime()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TM_TOTAL tm;
|
|
Toshihiro Shimizu |
890ddd |
TM_USER user;
|
|
Toshihiro Shimizu |
890ddd |
TM_SYSTEM system;
|
|
Toshihiro Shimizu |
890ddd |
getElapsedTime(tm, user, system);
|
|
Toshihiro Shimizu |
890ddd |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
return (TINT32)(user / 10000);
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
return (TINT32)(user * 1000) / STW_TICKS_PER_SECOND;
|
|
Toshihiro Shimizu |
890ddd |
#endif //_WIN32
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TUINT32 TStopWatch::getSystemTime()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TM_TOTAL tm;
|
|
Toshihiro Shimizu |
890ddd |
TM_USER user;
|
|
Toshihiro Shimizu |
890ddd |
TM_SYSTEM system;
|
|
Toshihiro Shimizu |
890ddd |
getElapsedTime(tm, user, system);
|
|
Toshihiro Shimizu |
890ddd |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
return (TINT32)(system / 10000);
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
return (TINT32)(system * 1000) / STW_TICKS_PER_SECOND;
|
|
Toshihiro Shimizu |
890ddd |
#endif //_WIN32
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TStopWatch::operator string()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
char buffer[256];
|
|
Toshihiro Shimizu |
890ddd |
ostrstream out(buffer, sizeof(buffer));
|
|
Toshihiro Shimizu |
890ddd |
out << m_name.c_str() << ": " << (int)getTotalTime() << " u" << (int)getUserTime() << " s" << (TINT32)getSystemTime();
|
|
Toshihiro Shimizu |
890ddd |
return string(buffer, out.pcount());
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void TStopWatch::print()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
print(cout);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void TStopWatch::print(ostream &out)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
string s(*this);
|
|
Toshihiro Shimizu |
890ddd |
out << s.c_str() << endl;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void TStopWatch::printGlobals(ostream &out)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
const int n = sizeof(StopWatch) / sizeof(StopWatch[0]);
|
|
Toshihiro Shimizu |
890ddd |
for (int i = 0; i < n; i++)
|
|
Toshihiro Shimizu |
890ddd |
if (StopWatch[i].m_active)
|
|
Toshihiro Shimizu |
890ddd |
StopWatch[i].print(out);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void TStopWatch::printGlobals()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
printGlobals(cout);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void dummyFunction()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// It's used just to calculate the overhead
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void determineTimer()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void (*pFunc)() = dummyFunction;
|
|
Toshihiro Shimizu |
890ddd |
// cout << "DETERMINE TIMER" << endl;
|
|
Toshihiro Shimizu |
890ddd |
// Assume the worst
|
|
Toshihiro Shimizu |
890ddd |
timerToUse = TTUTickCount;
|
|
Toshihiro Shimizu |
890ddd |
if (QueryPerformanceFrequency(&perfFreq)) {
|
|
Toshihiro Shimizu |
890ddd |
// We can use hires timer, determine overhead
|
|
Toshihiro Shimizu |
890ddd |
timerToUse = TTUHiRes;
|
|
Toshihiro Shimizu |
890ddd |
overheadTicks = 200;
|
|
Toshihiro Shimizu |
890ddd |
for (int i = 0; i < 20; i++) {
|
|
Toshihiro Shimizu |
890ddd |
LARGE_INTEGER b, e;
|
|
Toshihiro Shimizu |
890ddd |
int Ticks;
|
|
Toshihiro Shimizu |
890ddd |
QueryPerformanceCounter(&b);
|
|
Toshihiro Shimizu |
890ddd |
(*pFunc)();
|
|
Toshihiro Shimizu |
890ddd |
QueryPerformanceCounter(&e);
|
|
Toshihiro Shimizu |
890ddd |
Ticks = e.LowPart - b.LowPart;
|
|
Toshihiro Shimizu |
890ddd |
if (Ticks >= 0 && Ticks < overheadTicks)
|
|
Toshihiro Shimizu |
890ddd |
overheadTicks = Ticks;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
// See if Freq fits in 32 bits; if not lose some precision
|
|
Toshihiro Shimizu |
890ddd |
perfFreqAdjust = 0;
|
|
Toshihiro Shimizu |
890ddd |
int High32 = perfFreq.HighPart;
|
|
Toshihiro Shimizu |
890ddd |
while (High32) {
|
|
Toshihiro Shimizu |
890ddd |
High32 >>= 1;
|
|
Toshihiro Shimizu |
890ddd |
perfFreqAdjust++;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
void determineTimer()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
#endif
|