| 58 | } |
| 59 | |
| 60 | void Test_example_KeepIf_performance() |
| 61 | { |
| 62 | using namespace fplus; |
| 63 | |
| 64 | typedef std::vector<int> Ints; |
| 65 | auto run_loop = [&](const Ints values) { |
| 66 | Ints odds; |
| 67 | for (int x : values) |
| 68 | if (is_odd_int(x)) |
| 69 | odds.push_back(x); |
| 70 | return odds; |
| 71 | }; |
| 72 | auto run_stl = [&](const Ints values) { |
| 73 | Ints odds; |
| 74 | std::copy_if(std::begin(values), std::end(values), |
| 75 | std::back_inserter(odds), is_odd_int); |
| 76 | return odds; |
| 77 | }; |
| 78 | auto run_FunctionalPlus = [&](const Ints values) { return keep_if(is_odd_int, values); }; |
| 79 | |
| 80 | // make debug runs faster |
| 81 | #if defined NDEBUG || defined _DEBUG |
| 82 | std::size_t numRuns = 10; |
| 83 | #else |
| 84 | std::size_t numRuns = 20000; |
| 85 | #endif |
| 86 | |
| 87 | Ints values = generate<Ints>(rand, 5000); |
| 88 | run_n_times(run_loop, numRuns, "Hand-written for loop", values); |
| 89 | run_n_times(run_stl, numRuns, "std::copy_if", values); |
| 90 | run_n_times(run_FunctionalPlus, numRuns, "FunctionalPlus::keep_if", values); |
| 91 | } |
| 92 | |
| 93 | int main() |
| 94 | { |
no test coverage detected