Repository navigation
Expand file tree
/
Copy pathBenchmark.mpp
More file actions
65 lines (58 loc) · 2.07 KB
/
Copy pathBenchmark.mpp
File metadata and controls
65 lines (58 loc) · 2.07 KB
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
export module CppUtils.UnitTest.Benchmark;
import std;
import CppUtils.Chrono;
export namespace CppUtils::UnitTest
{
/// @brief Execution measurement result of a benchmarked test block.
struct BenchmarkResult final
{
std::string name; ///< Name or label identifying the benchmarked operation.
std::chrono::nanoseconds duration{0}; ///< Measured execution duration in nanoseconds.
/**
* @brief Formats the measured duration into a human-readable string (ns, us, ms, s).
* @return Formatted duration string.
*/
[[nodiscard]] auto durationString() const -> std::string
{
return Chrono::durationToString(duration);
}
};
/// @brief Comparison result between two benchmark measurements.
struct BenchmarkComparison final
{
BenchmarkResult first; ///< First benchmark measurement (baseline or candidate).
BenchmarkResult second; ///< Second benchmark measurement to compare against.
/**
* @brief Computes the duration ratio of the first benchmark relative to the second.
* @return Duration ratio (first / second).
*/
[[nodiscard]] constexpr auto ratio() const -> double
{
const auto firstCount = static_cast<double>(first.duration.count());
const auto secondCount = static_cast<double>(second.duration.count());
if (secondCount == 0.0)
return 1.0;
return firstCount / secondCount;
}
/**
* @brief Checks if the first benchmark executed faster than the second benchmark.
* @return True if first took less time than second.
*/
[[nodiscard]] constexpr auto isFirstFaster() const -> bool
{
return first.duration < second.duration;
}
/**
* @brief Computes the speedup multiplier (> 1.0) of the faster benchmark over the slower one.
* @return Speedup factor.
*/
[[nodiscard]] constexpr auto speedup() const -> double
{
const auto firstCount = static_cast<double>(first.duration.count());
const auto secondCount = static_cast<double>(second.duration.count());
if (firstCount == 0.0 or secondCount == 0.0)
return 1.0;
return isFirstFaster() ? (secondCount / firstCount) : (firstCount / secondCount);
}
};
}