LDMX Software
Public Member Functions | Private Types | Private Attributes | List of all members
framework::performance::Timer Class Reference

Time how long a specific operation takes and serialize the result with ROOT. More...

#include <Timer.h>

Public Member Functions

 Timer ()=default
 create a timer but don't start it yet
 
virtual ~Timer ()=default
 create defualt destructor
 
void reset ()
 reset a timer to un-started state without re-allocating
 
void start ()
 start the timer
 
void stop ()
 stop the timer
 
double duration () const
 retrieve the value of the duration in seconds
 
void write (TDirectory *location, const std::string &name) const
 Write ourselves under the input name to the input location.
 
 ClassDef (Timer, 1)
 

Private Types

using clock = std::chrono::high_resolution_clock
 

Private Attributes

std::chrono::time_point< clock > begin_
 The time_point when the timer is started.
 
std::chrono::time_point< clock > end_
 The time_point when the timer is stopped.
 
long int start_time_ {-1}
 Time stamp for when timer was started in nanoseconds since UNIX epoch.
 
double duration_ {-1}
 Length of time recorded by the timer in seconds.
 

Detailed Description

Time how long a specific operation takes and serialize the result with ROOT.

Under-the-hood, we use std::chrono::time_point and std::chrono::duration along with std::chrono::high_resolution_clock so we can clearly and faithfully time how long things take and transparently convert the resulting duration into seconds (including sub-second increments). Since ROOT doesn't have a dictionary for serializing these std::chrono classes (and I'm not interested in making one), I simply mark those helper-members as "transient" (have //! on the line they are declared in) so they are ignored by the dictionary generation. The fundamental members start_time_ and duration_ are what end up being written to disk.

Below is an example of reading the absolute timer from a test run of this performance logging infrastructure. As you can see, the two atomic types are available for reading but the std::chrono::time_point objects are not.

>>> import uproot
>>> with uproot.open('test_performance.root') as f:
... f['performance/absolute'].members
...
{'start_time_': 1701704424941358702, 'duration_': 0.078910213}

Definition at line 38 of file Timer.h.

Member Typedef Documentation

◆ clock

using framework::performance::Timer::clock = std::chrono::high_resolution_clock
private

Definition at line 39 of file Timer.h.

Member Function Documentation

◆ duration()

double framework::performance::Timer::duration ( ) const

retrieve the value of the duration in seconds

Definition at line 27 of file Timer.cxx.

27{ return duration_; }
double duration_
Length of time recorded by the timer in seconds.
Definition Timer.h:74

References duration_.

◆ reset()

void framework::performance::Timer::reset ( )

reset a timer to un-started state without re-allocating

Definition at line 8 of file Timer.cxx.

8 {
9 begin_ = {};
10 end_ = {};
11 start_time_ = -1;
12 duration_ = -1;
13}
long int start_time_
Time stamp for when timer was started in nanoseconds since UNIX epoch.
Definition Timer.h:63
std::chrono::time_point< clock > end_
The time_point when the timer is stopped.
Definition Timer.h:56
std::chrono::time_point< clock > begin_
The time_point when the timer is started.
Definition Timer.h:47

References begin_, duration_, end_, and start_time_.

◆ start()

void framework::performance::Timer::start ( )

start the timer

Definition at line 15 of file Timer.cxx.

15 {
16 begin_ = clock::now();
17 start_time_ = std::chrono::duration_cast<std::chrono::nanoseconds>(
18 begin_.time_since_epoch())
19 .count();
20}

References begin_, and start_time_.

Referenced by framework::performance::Tracker::absolute_start().

◆ stop()

void framework::performance::Timer::stop ( )

stop the timer

Definition at line 22 of file Timer.cxx.

22 {
23 end_ = clock::now();
24 duration_ = std::chrono::duration<double>(end_ - begin_).count();
25}

References begin_, duration_, and end_.

Referenced by framework::performance::Tracker::absolute_stop().

◆ write()

void framework::performance::Timer::write ( TDirectory *  location,
const std::string &  name 
) const

Write ourselves under the input name to the input location.

This is just here to avoid repeating the boiler-plate

location->WriteObject(&timer_obj, name.c_str());

Since I don't like seeing & or c_str() in my code.

Definition at line 29 of file Timer.cxx.

29 {
30 location->WriteObject(this, name.c_str());
31}

Referenced by framework::performance::Tracker::~Tracker().

Member Data Documentation

◆ begin_

std::chrono::time_point<clock> framework::performance::Timer::begin_
private

The time_point when the timer is started.

The comment beginning with //! is what marks this member as "transient" for ROOT I/O. not serialized, just for measurement purposes

Definition at line 47 of file Timer.h.

Referenced by reset(), start(), and stop().

◆ duration_

double framework::performance::Timer::duration_ {-1}
private

Length of time recorded by the timer in seconds.

Sub-second intervals are recorded as well (so if the timer ran for 1 millisecond you would see 0.001 stored in this member variable).

Set to -1 if timer was not ended

Definition at line 74 of file Timer.h.

74{-1};

Referenced by duration(), reset(), and stop().

◆ end_

std::chrono::time_point<clock> framework::performance::Timer::end_
private

The time_point when the timer is stopped.

The comment beginning with //! is what marks this member as "transient" for ROOT I/O. not serialized, just for measurement purposes

Definition at line 56 of file Timer.h.

Referenced by reset(), and stop().

◆ start_time_

long int framework::performance::Timer::start_time_ {-1}
private

Time stamp for when timer was started in nanoseconds since UNIX epoch.

Set to -1 if timer was not started

Definition at line 63 of file Timer.h.

63{-1};

Referenced by reset(), and start().


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