FML
Public Member Functions
FML::UTILS::Timings Class Reference

Class for performing timings of the code using std::chrono. More...

#include <Timings.h>

Public Member Functions

 Timings ()=default
 
void PrintAllTimings () const
 Output all the recorded timings. More...
 
void StartTiming (const char *name)
 Start timing. More...
 
void StartTiming (std::string name)
 Start timing. More...
 
double EndTiming (const char *name, bool print=false)
 End timing. More...
 
double EndTiming (std::string name, bool print=false)
 End timing and print to screen if print = true. More...
 
void PrintTotalTime (const char *name)
 Print to screen total time for a given label. More...
 
void PrintTotalTime (std::string name)
 Print to screen total time for a given label. More...
 

Detailed Description

Class for performing timings of the code using std::chrono.

Definition at line 22 of file Timings.h.

Constructor & Destructor Documentation

◆ Timings()

FML::UTILS::Timings::Timings ( )
default

Member Function Documentation

◆ EndTiming() [1/2]

double FML::UTILS::Timings::EndTiming ( const char *  name,
bool  print = false 
)
inline

End timing.

Parameters
[in]nameThe label to give to the timing
[in]printOptional: print timing to screen right away

Definition at line 83 of file Timings.h.

83{ return EndTiming(std::string(name), print); }
void print()
Definition: Main.cpp:11
double EndTiming(const char *name, bool print=false)
End timing.
Definition: Timings.h:83

◆ EndTiming() [2/2]

double FML::UTILS::Timings::EndTiming ( std::string  name,
bool  print = false 
)
inline

End timing and print to screen if print = true.

Parameters
[in]nameThe label to give to the timing
[in]printOptional: print timing to screen right away

Definition at line 89 of file Timings.h.

89 {
90 auto end_time = std::chrono::steady_clock::now();
91 std::lock_guard<std::mutex> guard(timings_mutex);
92
93 double time_sec = 0.0;
94 double tot_time_sec = 0.0;
95
96 // Find current time
97 auto it = timings.find(name);
98 if (it != timings.end()) {
99 auto start_time = it->second;
100 time_sec = timeInSeconds(start_time, end_time);
101 }
102
103 // Find total elapsed time
104 auto it2 = elapsed_time_sec.find(name);
105 if (it2 == elapsed_time_sec.end()) {
106 elapsed_time_sec[name] = time_sec;
107 } else {
108 it2->second += time_sec;
109 tot_time_sec = it2->second;
110 }
111
112 if (print) {
113 if (tot_time_sec == 0.0) {
114 if (time_sec == 0.0) {
115 std::cout << "Can't print elapsed time. Start time for time-point [" << name
116 << "] was not found\n";
117 } else {
118 std::cout << "Elapsed time for [" << name << "]: " << time_sec << " sec\n";
119 }
120 } else {
121 std::cout << "Current elapsed time for [" << name << "]: " << time_sec
122 << " sec. Total: " << tot_time_sec << " sec\n";
123 }
124 }
125
126 return time_sec;
127 }

◆ PrintAllTimings()

void FML::UTILS::Timings::PrintAllTimings ( ) const
inline

Output all the recorded timings.

Definition at line 41 of file Timings.h.

41 {
42#ifdef USE_MPI
43 int ThisTask = 0;
44 MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
45 if (ThisTask > 0)
46 return;
47#endif
48 std::cout << "\n";
49 std::cout << "============================================\n";
50 std::cout << "All the recorded timings for task 0:\n";
51 std::cout << "============================================\n";
52 for (const auto & t : elapsed_time_sec) {
53 std::cout << "Total elapsed time for [" << std::setw(35) << t.first << "]: " << std::setw(10)
54 << t.second << " sec\n";
55 }
56 std::cout << "============================================\n";
57 std::cout << "\n";
58 }
int ThisTask
The MPI task number.
Definition: Global.cpp:33

◆ PrintTotalTime() [1/2]

void FML::UTILS::Timings::PrintTotalTime ( const char *  name)
inline

Print to screen total time for a given label.

Parameters
[in]nameThe label to print the time of

Definition at line 132 of file Timings.h.

132{ PrintTotalTime(std::string(name)); }
void PrintTotalTime(const char *name)
Print to screen total time for a given label.
Definition: Timings.h:132

◆ PrintTotalTime() [2/2]

void FML::UTILS::Timings::PrintTotalTime ( std::string  name)
inline

Print to screen total time for a given label.

Parameters
[in]nameThe label to print the time of

Definition at line 137 of file Timings.h.

137 {
138 auto it = elapsed_time_sec.find(name);
139 if (it != elapsed_time_sec.end()) {
140 double time_sec = it->second;
141 std::cout << "Total elapsed time for [" << name << "]: " << time_sec << " sec\n";
142 }
143 }

◆ StartTiming() [1/2]

void FML::UTILS::Timings::StartTiming ( const char *  name)
inline

Start timing.

Parameters
[in]nameThe label to give to the timing

Definition at line 63 of file Timings.h.

63{ StartTiming(std::string(name)); }
void StartTiming(const char *name)
Start timing.
Definition: Timings.h:63

◆ StartTiming() [2/2]

void FML::UTILS::Timings::StartTiming ( std::string  name)
inline

Start timing.

Definition at line 66 of file Timings.h.

66 {
67 auto start_time = std::chrono::steady_clock::now();
68 std::lock_guard<std::mutex> guard(timings_mutex);
69
70 auto it = timings.find(name);
71 if (it != timings.end()) {
72 it->second = start_time;
73 } else {
74 timings[name] = start_time;
75 elapsed_time_sec[name] = 0.0;
76 }
77 }

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