Template Numerical Library: TNL::Timer Class Reference

Template Numerical Library version\ main:be7e16d88

Loading...
Searching...
No Matches
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 creating a new timer and resets it.
double | getCPUTime () const
| Returns the elapsed CPU time on this timer.
double | getRealTime () const
| Returns the elapsed real time on this timer.
void | reset ()
| Reset the CPU and real-time timers.
void | start ()
| Starts timer.
void | stop ()
| Stops (pauses) the CPU and the real-time timers, but does not set them to zero.
bool | writeLog (Logger &logger, int logLevel=0) const
| Writes a record into the logger.

## Protected Types
using | Duration = std::chrono::high_resolution_clock::duration
using | TimePoint = 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 double | readCPUTime ()
| Function for measuring the CPU time.
static TimePoint | readRealTime ()
| Function for measuring the real time.

## Protected Attributes
double | initialCPUTime
TimePoint | initialRealTime
bool | stopState
double | totalCPUTime
Duration | totalRealTime

## Detailed Description

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

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

Example
  #include <iostream>
  #include <chrono>
  #include <thread>
  #include <TNL/Timer.h>
  int
  main()
  {
  const int milliseconds = 0.5e3;
  TNL::Timer time;
  time.start();
  std::this_thread::sleep_for( std::chrono::milliseconds( milliseconds ) );
  time.stop();
  std::cout << "Elapsed real time: " << time.getRealTime() << '\n';
  std::cout << "Elapsed CPU time: " << time.getCPUTime() << '\n';
  time.reset();
  std::cout << "Real time after reset:" << time.getRealTime() << '\n';
  std::cout << "CPU time after reset: " << time.getCPUTime() << '\n';
  }
  std::cout
  TNL::Timer
  Class for real time, CPU time and CPU cycles measuring.
  Definition Timer.h:25
  std::chrono::milliseconds
  std::this_thread::sleep_for
  T sleep_for(T... args)

Output
  Elapsed real time: 0.500077
  Elapsed CPU time: 9e-06
  Real time after reset:0
  CPU time after reset: 0

## Member Function Documentation

## ◆ getCPUTime()

double TNL::Timer::getCPUTime | ( |  | ) | const
| nodiscard

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
| nodiscard

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.

## ◆ start()

void TNL::Timer::start | ( |  | ) |

Starts timer.

Starts the CPU and real-time timers. This method can be used also after using the stop method. The timer then continues measuring the time without resetting.

## ◆ writeLog()

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

Writes a record into the logger.

Parameters

  logger | Name of Logger object.
  logLevel | A non-negative integer recording the log record indent.

Example
  #include <iostream>
  #include <chrono>
  #include <thread>
  #include <TNL/Timer.h>
  #include <TNL/Logger.h>
  int
  main()
  {
  const int milliseconds = 0.5e3;
  TNL::Timer time;
  time.start();
  std::this_thread::sleep_for( std::chrono::milliseconds( milliseconds ) );
  time.stop();
  TNL::Logger logger( 50, std::cout );
  time.writeLog( logger, 0 );
  }
  TNL::Logger
  Creates calculations log in the form of a table.
  Definition Logger.h:15

Output
  | Real time: 0.500083 |
  | CPU time: 0 |

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

- src/TNL/Timer.h