File tree Expand file tree Collapse file tree 6 files changed +73
-3
lines changed
Expand file tree Collapse file tree 6 files changed +73
-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 44#include " detail/fused.hpp"
55#include " detail/lazy.hpp"
66#include " detail/products.hpp"
7+ #include " detail/reserved.hpp"
78
89#include < benchmark/benchmark.h>
910
@@ -57,8 +58,22 @@ static void BM_all_lazy(benchmark::State& state) {
5758 }
5859}
5960
61+ static void BM_all_reserved (benchmark::State& state) {
62+ const auto size = state.range (0 );
63+ const auto begin = global_products.begin ();
64+
65+ reserved::database db{.products = {begin, begin + size}};
66+
67+ for (auto _ : state) {
68+ for (std::string_view name : db.get_products ({.min_quantity = 10 })) {
69+ use (name);
70+ }
71+ }
72+ }
73+
6074BENCHMARK (BM_all_eager)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6175BENCHMARK (BM_all_fused)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6276BENCHMARK (BM_all_lazy)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
77+ BENCHMARK (BM_all_reserved)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6378
6479BENCHMARK_MAIN ();
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ auto eager::database::get_products(query_t query) const -> names_t {
77
88 for (const auto & product : products) {
99 if (product.quantity >= query.min_quantity ) {
10- results.push_back (product.name );
10+ results.emplace_back (product.name );
1111 }
1212 }
1313
Original file line number Diff line number Diff line change 1+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+ #include " reserved.hpp"
4+
5+ #include < algorithm>
6+
7+ auto reserved::database::get_products (query_t query) const -> names_t {
8+ const auto capacity = std::ranges::count_if (
9+ products, [=](const product_t & product) -> bool { return product.quantity >= query.min_quantity ; });
10+
11+ names_t results;
12+ results.reserve (capacity);
13+
14+ for (const auto & product : products) {
15+ if (product.quantity >= query.min_quantity ) {
16+ results.emplace_back (product.name );
17+ }
18+ }
19+
20+ return results;
21+ }
Original file line number Diff line number Diff line change 1+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+ #pragma once
4+
5+ #include " product.hpp"
6+
7+ #include < vector>
8+
9+ namespace reserved {
10+
11+ using names_t = std::vector<std::string_view>;
12+
13+ struct database {
14+ std::vector<product_t > products;
15+
16+ auto get_products (query_t ) const -> names_t;
17+ };
18+
19+ } // namespace reserved
Original file line number Diff line number Diff line change 44#include " detail/fused.hpp"
55#include " detail/lazy.hpp"
66#include " detail/products.hpp"
7+ #include " detail/reserved.hpp"
78
89#include < benchmark/benchmark.h>
910
@@ -57,8 +58,22 @@ static void BM_take_lazy(benchmark::State& state) {
5758 }
5859}
5960
61+ static void BM_take_reserved (benchmark::State& state) {
62+ const auto size = state.range (0 );
63+ const auto begin = global_products.begin ();
64+
65+ reserved::database db{.products = {begin, begin + size}};
66+
67+ for (auto _ : state) {
68+ for (std::string_view name : db.get_products ({.min_quantity = 10 }) | std::views::take (100 )) {
69+ use (name);
70+ }
71+ }
72+ }
73+
6074BENCHMARK (BM_take_eager)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6175BENCHMARK (BM_take_fused)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6276BENCHMARK (BM_take_lazy)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
77+ BENCHMARK (BM_take_reserved)->RangeMultiplier(2 )->Range(1 << 10 , max_size);
6378
6479BENCHMARK_MAIN ();
You can’t perform that action at this time.
0 commit comments