Benchmarked function : several sub parts of this function are benchmarked separately
| 26 | |
| 27 | // Benchmarked function : several sub parts of this function are benchmarked separately |
| 28 | void benchmark_example() |
| 29 | { |
| 30 | using Ints = std::vector<int>; |
| 31 | |
| 32 | // Example 1 : benchmark by replacing a function |
| 33 | // |
| 34 | // We want to benchmark the following code : |
| 35 | // Ints ascending_numbers = fplus::numbers(0, 1000); |
| 36 | // |
| 37 | // So, first we make an alternate version of the function "fplus::numbers" |
| 38 | // Since fplus::numbers is a template function, we need to specify |
| 39 | // that the actual version we want to benchmark |
| 40 | // is "fplus::numbers<int, std::vector<int>>" |
| 41 | // |
| 42 | // numbers_bench will our alternate version and it |
| 43 | // has the same signature as fplus::numbers<int, std::vector<int>>, |
| 44 | // except that it also stores stats into the benchmark session, |
| 45 | // under the name "numbers" |
| 46 | // |
| 47 | // Note that make_benchmark_function *will add side effects* to the function |
| 48 | // (since it stores data into the benchmark session at each call) |
| 49 | auto numbers_bench = make_benchmark_function( |
| 50 | my_benchmark_session, |
| 51 | "numbers", |
| 52 | fplus::numbers<int, std::vector<int>>); |
| 53 | // Then, we replace the original code "Ints ascending_numbers = fplus::numbers(0, 1000);" |
| 54 | // by a code that uses the benchmarked function |
| 55 | Ints ascending_numbers = numbers_bench(0, 100000); |
| 56 | |
| 57 | // Example 2: benchmark by replacing an expression |
| 58 | // Below, we will benchmark an expression |
| 59 | // The original expression we want to benchmark was: |
| 60 | // Ints shuffled_numbers = fplus::shuffle(std::mt19937::default_seed, ascending_numbers); |
| 61 | // |
| 62 | // In order to do so, we just copy/paste this expression |
| 63 | // into "bench_expression" like shown below. |
| 64 | // This expression will then be benchmarked with the name "shuffle" |
| 65 | // |
| 66 | // Notes : |
| 67 | // - benchmark_expression is a preprocessor macro that uses an immediately invoked lambda (IIL) |
| 68 | // - the expression can be paster as-is, and it is possible to not remove the ";" |
| 69 | // (although it also works if it is not present) |
| 70 | Ints shuffled_numbers = benchmark_expression( |
| 71 | my_benchmark_session, |
| 72 | "shuffle", |
| 73 | fplus::shuffle(std::mt19937::default_seed, ascending_numbers);); |
| 74 | |
| 75 | // Example 3: also benchmark by replacing an expression |
| 76 | // The original expression was |
| 77 | // const auto sorted_numbers = fplus::sort(shuffled_numbers); |
| 78 | const auto sorted_numbers = benchmark_expression( |
| 79 | my_benchmark_session, |
| 80 | "sort_shuffled_sequence", |
| 81 | fplus::sort(shuffled_numbers);); |
| 82 | // Verify that the sort has worked |
| 83 | assert(sorted_numbers == ascending_numbers); |
| 84 | |
| 85 | // In this toy example, we will compare the performance |
nothing calls this directly
no test coverage detected