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