tahoma2d/toonz/sources/include/tstopwatch.h

135 lines
4 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef T_STOPWATCH_INCLUDED
#define T_STOPWATCH_INCLUDED
#include "tcommon.h"
#undef DVAPI
#undef DVVAR
#ifdef TNZCORE_EXPORTS
#define DVAPI DV_EXPORT_API
#define DVVAR DV_EXPORT_VAR
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
#endif
#ifdef _WIN32
#include "windows.h"
#endif
//===============================================================
2016-06-15 18:43:10 +12:00
/**
2016-03-19 06:57:51 +13:00
* This is an example of how to use stopwatch.
*/
/*! The TStopWatch class allows you to recording time (in milliseconds)
taken by your process or by system to perform various tasks.
It is possible to record these times:
2016-06-15 18:43:10 +12:00
Total Time, User Time and System Time.
2016-03-19 06:57:51 +13:00
A stop watch is identified by a name that describes meaningfully its use.
*/
#ifdef _WIN32
#define TM_TOTAL DWORD
2016-06-15 18:43:10 +12:00
#define TM_USER \
__int64 // user time (time associated to the process calling stop
// watch)(unit=100-nanosecond)
#define TM_SYSTEM __int64 // system time (unit=100-nanosecond)
#define START DWORD // total starting reference time in milliseconds
#define START_USER \
FILETIME // process starting reference time (unit=100-nanosecond)
#define START_SYSTEM \
FILETIME // system starting reference time (unit=100-nanosecond)
2016-03-19 06:57:51 +13:00
#else
#define TM_TOTAL clock_t
#define TM_USER clock_t
#define TM_SYSTEM clock_t
#define START clock_t
#define START_USER clock_t
#define START_SYSTEM clock_t
#endif
2016-06-15 18:43:10 +12:00
class DVAPI TStopWatch {
std::string m_name; // stopwatch name
2016-03-19 06:57:51 +13:00
2016-06-20 14:23:05 +12:00
TM_TOTAL m_tm; // elapsed total time (in milliseconds)
TM_USER m_tmUser; // elapsed user time (time associated to the process
// calling stop watch)(unit=100-nanosecond)
2016-06-15 18:43:10 +12:00
TM_SYSTEM m_tmSystem; // elapsed system time (unit=100-nanosecond)
START m_start; // total starting reference time in milliseconds
START_USER
2016-06-20 14:23:05 +12:00
m_startUser; // process starting reference time (unit=100-nanosecond)
2016-06-15 18:43:10 +12:00
START_SYSTEM
2016-06-20 14:23:05 +12:00
m_startSystem; // system starting reference time (unit=100-nanosecond)
2016-03-19 06:57:51 +13:00
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-06-15 18:43:10 +12:00
LARGE_INTEGER m_hrStart; // high resolution starting reference (total) time
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
bool m_active; // sw e' stato usato (e' stato invocato il metodo start())
bool m_isRunning; // fra start() e stop()
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void setStartToCurrentTime();
void getElapsedTime(TM_TOTAL &tm, TM_USER &user, TM_SYSTEM &system);
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TStopWatch(std::string name = "");
~TStopWatch();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void reset();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
/*!Start the stop watch. If reset=true the stop watch is firstly reset.*/
void start(bool resetFlag = false);
void stop();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
/*!Returns the number of milliseconds that have elapsed in all the
start()-stop() intervals since
the stopWatch was reset up to the getTotalTime() call. The method can be
2016-06-15 18:43:10 +12:00
called during a start()-stop() interval */
TUINT32 getTotalTime();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
/*!Returns the amount of time that the process has spent executing application
* code. see getTotalTime() */
TUINT32 getUserTime();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
/*!Returns the amount of time that the process has spent executing operating
* system code. see getTotalTime() */
TUINT32 getSystemTime();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const std::string &getName() { return m_name; };
void setName(std::string name) { m_name = name; };
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
/*!Returns a string containing the recorded times.*/
operator std::string();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
/*!Print (to cout) the name and the relative total,user and system times of
* the stop watch. see getTotalTime()*/
void print(std::ostream &out);
void print();
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
static TStopWatch StopWatch[10];
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
/*!
TStopWatch::global(index) is a global array of 10 stop watches.
*/
static TStopWatch &global(int index) {
assert(0 <= index && index < 10);
return StopWatch[index];
};
/*!
2016-03-19 06:57:51 +13:00
Allows you to print the name and the relative total,
2016-06-15 18:43:10 +12:00
user and system times of all the active stop watches.
2016-03-19 06:57:51 +13:00
*/
2016-06-15 18:43:10 +12:00
static void printGlobals(std::ostream &out);
static void printGlobals();
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------
2016-06-15 18:43:10 +12:00
#endif // __T_STOPWATCH_INCLUDED