Created
May 22, 2020 14:51
-
-
Save rr-codes/9c729542b9486cb96f99bd4f30453d79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Created by Richard Robinson on 5/22/2020. | |
| // | |
| #ifndef BENCHMARK_H | |
| #define BENCHMARK_H | |
| #include <chrono> | |
| #include <ostream> | |
| #define MAKE_FUNC(x) [](){x}; | |
| namespace benchmark { | |
| using precision_ns = std::chrono::duration<long double, std::nano>; | |
| struct measurements { | |
| const precision_ns etlTime; | |
| const precision_ns stlTime; | |
| [[nodiscard]] constexpr auto max() const { | |
| return std::max(etlTime, stlTime); | |
| } | |
| [[nodiscard]] std::string to_string() const { | |
| char str[256]; | |
| sprintf(str, "%d,%d,%Lf,%Lf", (int) etlTime.count(), (int) stlTime.count(), | |
| etlTime / max(), stlTime / max() | |
| ); | |
| return str; | |
| } | |
| }; | |
| template<size_t Iterations> | |
| constexpr precision_ns measure(void (*fn)()) { | |
| const auto start = std::chrono::high_resolution_clock::now(); | |
| for (size_t i = 0; i < Iterations; ++i) { | |
| fn(); | |
| } | |
| const auto end = std::chrono::high_resolution_clock::now(); | |
| return std::chrono::duration_cast<precision_ns>((end - start) / Iterations); | |
| } | |
| template<size_t Iterations> | |
| constexpr measurements get_measurements(void (*etl)(), void (*stl)()) { | |
| const auto etlTime = measure<Iterations>(etl); | |
| const auto stlTime = measure<Iterations>(stl); | |
| return measurements{etlTime, stlTime}; | |
| } | |
| } | |
| #endif //BENCHMARK_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment