Created
December 5, 2016 11:27
-
-
Save goldsborough/34204af3b642f15d0d031bb886b36b9b to your computer and use it in GitHub Desktop.
A benchmarking macro for GoogleTest
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
| #define Test(TestSuiteName, TestCaseName) void TestSuiteName##_##TestCaseName() | |
| #define Benchmark_N(TestSuiteName, TestCaseName, numberOfIterations) \ | |
| void Benchmark_##TestSuiteName##_##TestCaseName(); \ | |
| Test(TestSuiteName, TestCaseName) {\ | |
| typedef std::chrono::high_resolution_clock clock_t;\ | |
| typedef std::chrono::duration<double, std::micro> duration_t;\ | |
| double totalDuration = 0; \ | |
| double squaredDuration = 0;\ | |
| for (std::size_t i = 0; i < numberOfIterations; ++i) {\ | |
| clock_t::time_point start = clock_t::now();\ | |
| Benchmark_##TestSuiteName##_##TestCaseName();\ | |
| clock_t::time_point end = clock_t::now();\ | |
| duration_t duration = std::chrono::duration_cast<duration_t>(end - start);\ | |
| double microseconds = duration.count();\ | |
| totalDuration += microseconds;\ | |
| squaredDuration += (microseconds * microseconds);\ | |
| }\ | |
| double average = totalDuration / numberOfIterations;\ | |
| double variance = (squaredDuration / numberOfIterations) - (average * average);\ | |
| double standardDeviation = std::sqrt(variance);\ | |
| totalDuration /= 1e6;\ | |
| std::cout\ | |
| << "Total execution time: " << totalDuration << " s | "\ | |
| << "Average execution time: " << average << " us | "\ | |
| << "Standard deviation: " << standardDeviation << " us"\ | |
| << std::endl;\ | |
| }\ | |
| void Benchmark_##TestSuiteName##_##TestCaseName() | |
| #define Benchmark(TestSuiteName, TestCaseName) Benchmark_N(TestSuiteName, TestCaseName, 1000000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment