File tree Expand file tree Collapse file tree 6 files changed +69
-3
lines changed
Expand file tree Collapse file tree 6 files changed +69
-3
lines changed Original file line number Diff line number Diff line change @@ -29,8 +29,8 @@ function(beman_add_test)
2929 gtest_discover_tests(${target} )
3030endfunction ()
3131
32- beman_add_benchmark(TARGET all SOURCES all .benchmark.cpp detail/eager.cpp detail/fused.cpp detail/lazy.cpp detail/products.cpp)
33- beman_add_benchmark(TARGET take SOURCES take.benchmark.cpp detail/eager.cpp detail/fused.cpp detail/lazy.cpp detail/products.cpp)
32+ beman_add_benchmark(TARGET all SOURCES all .benchmark.cpp detail/eager.cpp detail/fused.cpp detail/lazy.cpp detail/products.cpp detail/reserved.cpp )
33+ beman_add_benchmark(TARGET take SOURCES take.benchmark.cpp detail/eager.cpp detail/fused.cpp detail/lazy.cpp detail/products.cpp detail/reserved.cpp )
3434
3535beman_add_test(TARGET concepts SOURCES concepts.test .cpp)
3636beman_add_test(TARGET constexpr SOURCES constexpr.test .cpp)
Original file line number Diff line number Diff line change 22#include " detail/fused.hpp"
33#include " detail/lazy.hpp"
44#include " detail/products.hpp"
5+ #include " detail/reserved.hpp"
56
67#include < benchmark/benchmark.h>
78
@@ -55,8 +56,22 @@ static void BM_all_lazy(benchmark::State& state) {
5556 }
5657}
5758
59+ static void BM_all_reserved (benchmark::State& state) {
60+ const auto size = state.range (0 );
61+ const auto begin = global_products.begin ();
62+
63+ reserved::database db{.products = {begin, begin + size}};
64+
65+ for (auto _ : state) {
66+ for (std::string_view name : db.get_products ({.min_quantity = 10 })) {
67+ use (name);
68+ }
69+ }
70+ }
71+
5872BENCHMARK (BM_all_eager)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
5973BENCHMARK (BM_all_fused)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6074BENCHMARK (BM_all_lazy)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
75+ BENCHMARK (BM_all_reserved)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6176
6277BENCHMARK_MAIN ();
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ auto eager::database::get_products(query_t query) const -> names_t {
55
66 for (const auto & product : products) {
77 if (product.quantity >= query.min_quantity ) {
8- results.push_back (product.name );
8+ results.emplace_back (product.name );
99 }
1010 }
1111
Original file line number Diff line number Diff line change 1+ #include " reserved.hpp"
2+
3+ #include < algorithm>
4+
5+ auto reserved::database::get_products (query_t query) const -> names_t {
6+ const auto capacity = std::ranges::count_if (
7+ products, [=](const product_t & product) -> bool { return product.quantity >= query.min_quantity ; });
8+
9+ names_t results;
10+ results.reserve (capacity);
11+
12+ for (const auto & product : products) {
13+ if (product.quantity >= query.min_quantity ) {
14+ results.emplace_back (product.name );
15+ }
16+ }
17+
18+ return results;
19+ }
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include " product.hpp"
4+
5+ #include < vector>
6+
7+ namespace reserved {
8+
9+ using names_t = std::vector<std::string_view>;
10+
11+ struct database {
12+ std::vector<product_t > products;
13+
14+ auto get_products (query_t ) const -> names_t;
15+ };
16+
17+ } // namespace reserved
Original file line number Diff line number Diff line change 22#include " detail/fused.hpp"
33#include " detail/lazy.hpp"
44#include " detail/products.hpp"
5+ #include " detail/reserved.hpp"
56
67#include < benchmark/benchmark.h>
78
@@ -55,8 +56,22 @@ static void BM_take_lazy(benchmark::State& state) {
5556 }
5657}
5758
59+ static void BM_take_reserved (benchmark::State& state) {
60+ const auto size = state.range (0 );
61+ const auto begin = global_products.begin ();
62+
63+ reserved::database db{.products = {begin, begin + size}};
64+
65+ for (auto _ : state) {
66+ for (std::string_view name : db.get_products ({.min_quantity = 10 }) | std::views::take (100 )) {
67+ use (name);
68+ }
69+ }
70+ }
71+
5872BENCHMARK (BM_take_eager)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
5973BENCHMARK (BM_take_fused)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6074BENCHMARK (BM_take_lazy)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
75+ BENCHMARK (BM_take_reserved)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6176
6277BENCHMARK_MAIN ();
You can’t perform that action at this time.
0 commit comments