3ds Max C++ API Reference
StopWatch Class Reference

A basic timer tool. More...

#include <StopWatch.h>

+ Inheritance diagram for StopWatch:

Public Types

enum  TimerState { PLAYING =0 , PAUSED , STOPPED }
 Three timer states are defined. More...
 

Public Member Functions

UtilExport StopWatch ()
 Construct the StopWatch Object. More...
 
UtilExport void Start ()
 This method lets you start to trace the time and changes the current state to PLAYING. More...
 
UtilExport void StartPaused ()
 This method lets you start in a PAUSED state. More...
 
UtilExport void Pause ()
 This method lets you pause to trace the time and changes the current state to PAUSED. More...
 
UtilExport void Resume ()
 This method lets you resume to trace the time and changes the current state to PLAYING. More...
 
UtilExport void Stop ()
 This method lets you stop to trace the time and changes the current state to STOPPED. More...
 
UtilExport void Reset ()
 This method lets you clear the time traced and changes the current state to STOPPED. More...
 
UtilExport bool IsRunning () const
 This method lets you know whether the timer is running. More...
 
UtilExport bool IsPaused () const
 This method lets you know whether the timer is paused. More...
 
UtilExport bool IsStopped () const
 This method lets you know whether the timer is stopped. More...
 
UtilExport TimerState GetTimerCurrentState () const
 This method lets you know what the current timer state is. More...
 
UtilExport double GetElapsedTime () const
 This method lets you get the elapsed time(milliseconds) traced. More...
 
UtilExport void GetLocalTime (SYSTEMTIME *time) const
 Retrieves the current local time. More...
 

Static Public Member Functions

static UtilExport double CalculateAverageOverhead (int loopCount)
 Calculates and returns the average overhead associated with Start/Stop and Pause/Resume. More...
 
- Static Public Member Functions inherited from MaxHeapOperators
static UtilExport voidoperator new (size_t size)
 Standard new operator used to allocate objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e)
 Standard new operator used to allocate objects if there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate objects that takes the type of memory, filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate arrays of objects. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport void operator delete (void *ptr)
 Standard delete operator used to deallocate an object If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an object If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an object that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, void *placement_ptr)
 Placement new operator. More...
 
static UtilExport void operator delete (void *ptr, void *placement_ptr)
 Placement delete operator. More...
 
static UtilExport voidaligned_malloc (size_t size, size_t alignment)
 Allocates memory on a specified alignment boundary. More...
 
static UtilExport voidaligned_realloc (void *ptr, size_t size, size_t alignment)
 Reallocates memory on a specified alignment boundary. More...
 
static UtilExport void aligned_free (void *ptr)
 Frees a block of memory that was allocated with aligned_malloc/aligned_realloc. More...
 

Detailed Description

A basic timer tool.

A StopWatch is a light-weight timer tool that tests the time consumption.This timer tool uses high performance counter(QueryPerformanceCounter) to record the time comsumption and the time tested is at the milliseconds level.The workflow of StopWatch is like this: Start -> ( Pause ->(Resume)*n)*n -> Stop. Between Start and Stop, numbers of Pause -> (Resume)*n operations can be done. And also Stop operation can be done directly after Pause operation. For example:

void theFunctionTested()
{
StopWatch watch;
watch.Start();
..... // the part that the user wants to time
watch.Pause();
..... // the part that the user doesn't want to time
watch.Resume();
..... // the part that the user wants to time
watch.Pause();
..... // the part that the user doesn't want to time
watch.Resume();
..... // the part that the user wants to time
watch.Pause();
..... // the part that the user doesn't want to time
watch.Stop(); // Stop operation can be done directly after Pause operation done
.....
double ms = watch.GetElapsedTime(); // get the consumption time that the user wants in the loop
}
UtilExport StopWatch()
Construct the StopWatch Object.

Member Enumeration Documentation

◆ TimerState

enum TimerState

Three timer states are defined.

Enumerator
PLAYING 

If timer is running, the state is defined as PLAYING and means that the timer is timing now.

PAUSED 

If timer is paused, the state is defined as PAUSED and means that the timer pauses to time unless doing Resume.

STOPPED 

If timer is stopped, the state is defined as STOPPED and means that the timer stops to time unless doing Start.

56  {
58  PLAYING =0,
60  PAUSED ,
62  STOPPED
63  };
@ STOPPED
If timer is stopped, the state is defined as STOPPED and means that the timer stops to time unless do...
Definition: StopWatch.h:62
@ PAUSED
If timer is paused, the state is defined as PAUSED and means that the timer pauses to time unless doi...
Definition: StopWatch.h:60
@ PLAYING
If timer is running, the state is defined as PLAYING and means that the timer is timing now.
Definition: StopWatch.h:58

Constructor & Destructor Documentation

◆ StopWatch()

Construct the StopWatch Object.

Member Function Documentation

◆ Start()

UtilExport void Start ( )

This method lets you start to trace the time and changes the current state to PLAYING.

◆ StartPaused()

UtilExport void StartPaused ( )

This method lets you start in a PAUSED state.

◆ Pause()

UtilExport void Pause ( )

This method lets you pause to trace the time and changes the current state to PAUSED.

◆ Resume()

UtilExport void Resume ( )

This method lets you resume to trace the time and changes the current state to PLAYING.

◆ Stop()

UtilExport void Stop ( )

This method lets you stop to trace the time and changes the current state to STOPPED.

◆ Reset()

UtilExport void Reset ( )

This method lets you clear the time traced and changes the current state to STOPPED.

◆ IsRunning()

UtilExport bool IsRunning ( ) const

This method lets you know whether the timer is running.

◆ IsPaused()

UtilExport bool IsPaused ( ) const

This method lets you know whether the timer is paused.

◆ IsStopped()

UtilExport bool IsStopped ( ) const

This method lets you know whether the timer is stopped.

◆ GetTimerCurrentState()

UtilExport TimerState GetTimerCurrentState ( ) const

This method lets you know what the current timer state is.

◆ GetElapsedTime()

UtilExport double GetElapsedTime ( ) const

This method lets you get the elapsed time(milliseconds) traced.

Note that each paired call of Start/Stop and Resume/Pause imposes some overhead, mainly the time associated with 2 calls to QueryPerformanceCounter. If client code is making many Resume/Pause calls and/or the code executing between the Start/Stop and Resume/Pause calls runs very fast, the reported elapsed time may be significantly impacted by this overhead.

See also
CalculateAverageOverhead(int loopCount)

◆ GetLocalTime()

UtilExport void GetLocalTime ( SYSTEMTIME *  time) const

Retrieves the current local time.

◆ CalculateAverageOverhead()

static UtilExport double CalculateAverageOverhead ( int  loopCount)
static

Calculates and returns the average overhead associated with Start/Stop and Pause/Resume.

Each paired call of Start/Stop and Resume/Pause imposes some overhead. The overhead is mainly the time associated with 2 calls to QueryPerformanceCounter. On average, the overhead is small (~ 1.6E-5 msec), but this can be significant if Pause/Resume is called many, many times while measuring something that runs very fast. The actual time associated with 2 calls to QueryPerformanceCounter is noisy, and it is not practical to correct for this overhead in the StopWatch code. Thus it is the responsibility of the client code to handle this if need be. Example:

size_t PTell_count = 0;
MaxSDK::Util::StopWatch PTell_stopwatch;
PTell_stopwatch.StartPaused();
while (...)
{
++PTell_count;
PTell_stopwatch.Resume();
...
PTell_stopwatch.Pause();
}
PTell_stopwatch.Stop();
DebugPrint(_T("Tell: %llu - %lf\n"), PTell_count, (PTell_stopwatch.GetElapsedTime() - PTell_count*timerOverhead));
A basic timer tool.
Definition: StopWatch.h:52
UtilExport void Pause()
This method lets you pause to trace the time and changes the current state to PAUSED.
UtilExport void Resume()
This method lets you resume to trace the time and changes the current state to PLAYING.
UtilExport void Stop()
This method lets you stop to trace the time and changes the current state to STOPPED.
UtilExport double GetElapsedTime() const
This method lets you get the elapsed time(milliseconds) traced.
UtilExport void StartPaused()
This method lets you start in a PAUSED state.
static UtilExport double CalculateAverageOverhead(int loopCount)
Calculates and returns the average overhead associated with Start/Stop and Pause/Resume.
UtilExport void DebugPrint(const MCHAR *format,...)
Parameters
loopCountThe number of loops calling Pause/Resume to average overhead over.
Returns
The average overhead of calling Start/Stop and Pause/Resume.