-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimekeeper.hpp
119 lines (108 loc) · 4.15 KB
/
timekeeper.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @file timekeeper.hpp
* @author Brendan Fallon (fallonbr@mcmaster.ca) (https://github.com/bren007pie)
* @brief This module contains chrono based class for timing and NPC race statistics functions.
* @version 1.0
* @date January 10, 2022
* @copyright Copyright (c) 2022 Brendan Fallon
*/
//// Preprocessor Directives ////
#pragma once
#include <chrono> // std::chrono::steady_clock, std::chrono::time_point, std::chrono::duration
#include <vector> // std::vector
#include <numeric> // std::accumulate
#include <cmath>
namespace NPC_Racer
{
//// --------- ////
//// Functions ////
//// --------- ////
/**
* @brief Takes the average of a series of pathfinding trial times.
*
* @param trial_times A collection of all the run times from each trial.
*
* @return Returns the population mean or average.
*/
double run_average(const std::vector<double> &trial_times)
{
return (std::accumulate(trial_times.begin(), trial_times.end(), 0.0) / (double)trial_times.size());
}
/**
* @brief Takes the population standard deviation of a series of pathfinding trial times.
*
* @param trial_times A collection of all the run times from each trial.
*
* @return Returns the population standard deviation of the series of trials.
*/
double run_standard_deviation(const std::vector<double> &trial_times)
{
// inspired by: https://www.programiz.com/cpp-programming/examples/standard-deviation
// recalculating the mean to avoid user error of having to re-input
const double mean = std::accumulate(trial_times.begin(), trial_times.end(), 0.0) / (double)trial_times.size();
double standard_deviation = 0.0;
for (size_t i = 0; i < trial_times.size(); i++)
{
standard_deviation += std::pow(trial_times[i] - mean, 2); // taking the some of the squares
}
return std::sqrt(standard_deviation / (double)trial_times.size());
}
/**
* @brief Takes the percentage difference between run averages.
*
* @param bigger The longer of the two trials.
* @param smaller The shorter of the two trials.
*
* @return Returns the percentage difference as a percentage (i.e. decimal x 100).
* @note Percentage difference definition: https://www.calculatorsoup.com/calculators/algebra/percent-difference-calculator.php
*/
double run_percentage_difference(const double &bigger, const double &smaller)
{
double mean = (bigger + smaller) / 2;
double percentage_difference = std::abs(bigger - smaller) / mean; // abs just in case user passed wrong input
return (percentage_difference * 100);
}
//// ----------------------- ////
//// Timekeeper Class ////
//// ----------------------- ////
/**
* @brief Starts timers and displays statistics
* @cite Adapted from https://baraksh.com/CSE701/notes.php#interlude-measuring-performance-with-chrono
*/
class timekeeper
{
public:
/**
* @brief Records the start time of the race when called.
*/
void start()
{
start_time = std::chrono::steady_clock::now();
}
/**
* @brief Records the start end of the race when called.
*/
void end()
{
elapsed_time = std::chrono::steady_clock::now() - start_time;
}
/**
* @brief Gives the time of the race in seconds.
* @return The elapsed time of the race as a double.
*/
double race_time() const
{
return elapsed_time.count();
}
private:
/**
* @param start_time The start time point for the race.
* @note When NPC_Racer::timekeeper object is initialized the start time is recorded but can be overridden with NPC_Racer::timekeeper::start()
*/
std::chrono::time_point<std::chrono::steady_clock> start_time = std::chrono::steady_clock::now();
/**
* @param elapsed_time The elapsed time duration as a double in seconds.
*/
std::chrono::duration<double> elapsed_time = std::chrono::duration<double>::zero();
};
}