Agrarsense
Public Member Functions | Private Attributes | List of all members
SimpleTimer Class Reference

#include <SimpleTimer.h>

Public Member Functions

 SimpleTimer ()
 
void Reset ()
 
void TakeTimeStamp ()
 
void StopTimer (FString prefix="Code executed in ", FString suffix=" milliseconds", bool printToScreen=false)
 
double GetAverageTime ()
 
double PrintAverageTime (FString prefix="Average execution time ", FString suffix=" milliseconds", bool printToScreen=false)
 
void ResetAverageTime ()
 

Private Attributes

double startTime
 
double totalMilliseconds = 0.0
 
int counter = 0
 

Detailed Description

Simple milliseconds timer for quick performance or other timing testing. Should only be used for local testing.


To measure execution time of some code block:

  1. Add include to the .cpp or .h file #include "Agrarsense/Debugging/SimpleTimer.h"
  2. Create timer auto timer = SimpleTimer(); // code to measure here timer.StopTimer();
  3. If you want to reset the same timer and continue using to measure other code block timer.Reset(); // more code to measure here timer.StopTimer(false, "more things to measure: ", " ms");

To measure average execution time of some code block:

  1. Add include to .h file #include "Agrarsense/Debugging/SimpleTimer.h
  2. Create SimpleTimer variable to .h file SimpleTimer timer;
  3. Init timer somewhere, for example in the BeginPlay timer = SimpleTimer();
  4. To measure average time of some code block timer.TakeTimeStamp(); // code to measure here timer.PrintAverageTime();

Definition at line 49 of file SimpleTimer.h.

Constructor & Destructor Documentation

◆ SimpleTimer()

SimpleTimer::SimpleTimer ( )
inline

Start SimpleTimer

Definition at line 57 of file SimpleTimer.h.

58 {
60 }
void TakeTimeStamp()
Definition: SimpleTimer.h:76

References TakeTimeStamp().

Member Function Documentation

◆ GetAverageTime()

double SimpleTimer::GetAverageTime ( )
inline

Get average time without printing it.

Definition at line 105 of file SimpleTimer.h.

106 {
107 double endTime = FPlatformTime::Seconds();
108 float milliseconds = (endTime - startTime) * 1000;
109
110 totalMilliseconds += milliseconds;
111 ++counter;
112 double currentAvg = totalMilliseconds / counter;
113
114 return currentAvg;
115 }
double totalMilliseconds
Definition: SimpleTimer.h:150
double startTime
Definition: SimpleTimer.h:149

References counter, startTime, and totalMilliseconds.

Referenced by PrintAverageTime().

◆ PrintAverageTime()

double SimpleTimer::PrintAverageTime ( FString  prefix = "Average execution time ",
FString  suffix = " milliseconds",
bool  printToScreen = false 
)
inline

print current average execution time.

Parameters
printToScreenshould message be printed on the screen. Default false.
prefixMessage prefix as FString.
suffixMessage suffix as FString.

Definition at line 123 of file SimpleTimer.h.

124 {
125 double currentAvg = GetAverageTime();
126
127 FString msString = FString::SanitizeFloat(currentAvg);
128 FString msg = prefix + msString + suffix;
129
130#if WITH_EDITOR
131 UE_LOG(LogTemp, Warning, TEXT("%s"), *msg);
132 if (GEngine && printToScreen)
133 {
134 GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("%s"), *msg));
135 }
136#endif
137
138 return currentAvg;
139 }
double GetAverageTime()
Definition: SimpleTimer.h:105

References GetAverageTime().

Referenced by ALidarManager::TickParallel().

◆ Reset()

void SimpleTimer::Reset ( )
inline

Reset timer

Definition at line 65 of file SimpleTimer.h.

66 {
68
70 counter = 0;
71 }

References counter, TakeTimeStamp(), and totalMilliseconds.

◆ ResetAverageTime()

void SimpleTimer::ResetAverageTime ( )
inline

Definition at line 141 of file SimpleTimer.h.

142 {
143 totalMilliseconds = 0.0;
144 counter = 0;
145 }

References counter, and totalMilliseconds.

Referenced by ALidarManager::RemoveRaycastLidar().

◆ StopTimer()

void SimpleTimer::StopTimer ( FString  prefix = "Code executed in ",
FString  suffix = " milliseconds",
bool  printToScreen = false 
)
inline

Stop the timer and print result.

Parameters
printToScreenshould message be printed on the screen. Default false.
prefixMessage prefix as FString.
suffixMessage suffix as FString.

Definition at line 87 of file SimpleTimer.h.

88 {
89 double endTime = FPlatformTime::Seconds();
90 float milliseconds = (endTime - startTime) * 1000;
91
92 FString msString = FString::SanitizeFloat(milliseconds);
93 FString msg = prefix + msString + suffix;
94 UE_LOG(LogTemp, Warning, TEXT("%s"), *msg);
95
96 if (GEngine && printToScreen)
97 {
98 GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("%s"), *msg));
99 }
100 }

References startTime.

◆ TakeTimeStamp()

void SimpleTimer::TakeTimeStamp ( )
inline

Take current timestamp

Definition at line 76 of file SimpleTimer.h.

77 {
78 startTime = FPlatformTime::Seconds();
79 }

References startTime.

Referenced by Reset(), SimpleTimer(), and ALidarManager::TickParallel().

Member Data Documentation

◆ counter

int SimpleTimer::counter = 0
private

Definition at line 151 of file SimpleTimer.h.

Referenced by GetAverageTime(), Reset(), and ResetAverageTime().

◆ startTime

double SimpleTimer::startTime
private

Definition at line 149 of file SimpleTimer.h.

Referenced by GetAverageTime(), StopTimer(), and TakeTimeStamp().

◆ totalMilliseconds

double SimpleTimer::totalMilliseconds = 0.0
private

Definition at line 150 of file SimpleTimer.h.

Referenced by GetAverageTime(), Reset(), and ResetAverageTime().


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