Template Numerical Library version main:e6bb26f
Public Member Functions | Protected Types | Static Protected Member Functions | Protected Attributes | List of all members
TNL::Timer Class Reference

Class for real time, CPU time and CPU cycles measuring. More...

#include <TNL/Timer.h>

Public Member Functions

 Timer ()
 Basic constructor. More...
 
unsigned long long int getCPUCycles () const
 Returns the number of CPU cycles (machine cycles) elapsed on this timer. More...
 
double getCPUTime () const
 Returns the elapsed CPU time on this timer. More...
 
double getRealTime () const
 Returns the elapsed real time on this timer. More...
 
void reset ()
 Resets timer. More...
 
void start ()
 Starts timer. More...
 
void stop ()
 Stops (pauses) the timer. More...
 
bool writeLog (Logger &logger, int logLevel=0) const
 Writes a record into the logger. More...
 

Protected Types

using Duration = typename std::chrono::high_resolution_clock::duration
 
using TimePoint = typename std::chrono::high_resolution_clock::time_point
 

Static Protected Member Functions

static double durationToDouble (const Duration &duration)
 Converts the real time into seconds as a floating point number.
 
static unsigned long long rdtsc ()
 Time Stamp Counter returning number of CPU cycles since reset. More...
 
static unsigned long long int readCPUCycles ()
 Function for counting the number of CPU cycles (machine cycles).
 
static double readCPUTime ()
 Function for measuring the CPU time.
 
static TimePoint readRealTime ()
 Function for measuring the real time.
 

Protected Attributes

unsigned long long int initialCPUCycles
 
double initialCPUTime
 
TimePoint initialRealTime
 
bool stopState
 
unsigned long long int totalCPUCycles
 
double totalCPUTime
 
Duration totalRealTime
 

Detailed Description

Class for real time, CPU time and CPU cycles measuring.

It measures the elapsed real time, CPU time (in seconds) and CPU cycles elapsed on the timer. The timer can be paused by calling stop and start methods and reseted by calling reset.

Example
#include <iostream>
#include <TNL/Timer.h>
using namespace TNL;
int main()
{
unsigned int microseconds = 0.5e6;
time.start();
usleep(microseconds);
time.stop();
std::cout << "Elapsed real time: " << time.getRealTime() << std::endl;
std::cout << "Elapsed CPU time: " << time.getCPUTime() << std::endl;
std::cout << "Elapsed CPU cycles: " << time.getCPUCycles() << std::endl;
time.reset();
std::cout << "Real time after reset:" << time.getRealTime() << std::endl;
std::cout << "CPU time after reset: " << time.getCPUTime() << std::endl;
std::cout << "CPU cycles after reset: " << time.getCPUCycles() << std::endl;
}
Timer()
Basic constructor.
Definition: Timer.hpp:20
T endl(T... args)
The main TNL namespace.
Definition: AtomicOperations.h:13
T time(T... args)
Output
Elapsed real time: 0.500075
Elapsed CPU time: 1.4e-05
Elapsed CPU cycles: 1650227304
Real time after reset:0
CPU time after reset: 0
CPU cycles after reset: 0

Constructor & Destructor Documentation

◆ Timer()

TNL::Timer::Timer ( )
inline

Basic constructor.

This function creates a new timer and resets it.

Member Function Documentation

◆ getCPUCycles()

unsigned long long int TNL::Timer::getCPUCycles ( ) const
inline

Returns the number of CPU cycles (machine cycles) elapsed on this timer.

CPU cycles are counted by adding the number of CPU cycles between start and stop methods together.

◆ getCPUTime()

double TNL::Timer::getCPUTime ( ) const
inline

Returns the elapsed CPU time on this timer.

This method returns the CPU time (i.e. time the CPU spent by processing this process) elapsed so far (in seconds). This method can be called while the timer is running, there is no need to use stop method first.

◆ getRealTime()

double TNL::Timer::getRealTime ( ) const
inline

Returns the elapsed real time on this timer.

This method returns the real time elapsed so far (in seconds). This method can be called while the timer is running, there is no need to use stop method first.

◆ rdtsc()

unsigned long long TNL::Timer::rdtsc ( )
inlinestaticprotected

Time Stamp Counter returning number of CPU cycles since reset.

Only for x86 compatible CPUs.

◆ reset()

void TNL::Timer::reset ( )
inline

Resets timer.

Resets all time and cycle measurements such as real time, CPU time and CPU cycles. Sets all of them to zero.

◆ start()

void TNL::Timer::start ( )
inline

Starts timer.

Starts all time and cycle measurements such as real time, CPU time and CPU cycles. This method can be used also after using the stop method. The timer then continues measuring the time without reseting.

◆ stop()

void TNL::Timer::stop ( )
inline

Stops (pauses) the timer.

Pauses all time and cycle measurements such as real time, CPU time and CPU cycles, but does not set them to zero.

◆ writeLog()

bool TNL::Timer::writeLog ( Logger logger,
int  logLevel = 0 
) const
inline

Writes a record into the logger.

Parameters
loggerName of Logger object.
logLevelA non-negative integer recording the log record indent.
Example
#include <iostream>
#include <TNL/Timer.h>
#include <TNL/Logger.h>
#include <unistd.h>
using namespace TNL;
int main()
{
unsigned int microseconds = 0.5e6;
time.start();
usleep(microseconds);
time.stop();
Logger logger( 50, std::cout );
time.writeLog( logger, 0 );
}
Output
| Real time: 0.501435 |
| CPU time: 1.9e-05 |
| CPU Cycles: 1654712529 |

The documentation for this class was generated from the following files: