Index: benchmarks/algorithms.bench.cpp =================================================================== --- benchmarks/algorithms.bench.cpp +++ benchmarks/algorithms.bench.cpp @@ -58,5 +58,84 @@ BENCHMARK_CAPTURE(BM_Sort, single_element_strings, getDuplicateStringInputs)->Arg(TestNumInputs); +template +void do_binary_search_benchmark(benchmark::State& st, GenInputs gen, Alg alg) +{ + using ValueType = typename decltype(gen(0))::value_type; + auto in = gen(st.range(0)); + std::sort(in.begin(), in.end()); + + const auto every_10_percentile = [&]() -> std::vector { + size_t step = in.size() / 10; + + if (step == 0) { + st.SkipWithError("Input doesn't contain enough elements"); + return {}; + } + + std::vector res; + for (size_t i = 0; i < in.size(); i += step) + res.push_back(&in[i]); + + return res; + }(); + + for (auto _ : st) + { + for (auto* test : every_10_percentile) + benchmark::DoNotOptimize(alg(in.begin(), in.end(), *test)); + } +} + +template +void BM_LowerBound(benchmark::State& st, GenInputs gen) +{ + do_binary_search_benchmark(st, gen, [](auto f, auto l, const auto& v) { + return std::lower_bound(f, l, v); + }); +} + +void BM_LowerBoundSeanParent(benchmark::State& state) { + int a[1024]; + std::iota(std::begin(a), std::end(a), 0); + // Code before the loop is not measured + for (auto _ : state) { + auto p = std::lower_bound(std::begin(a), std::end(a), 272291); + // Make sure the variable is not optimized away by compiler + benchmark::DoNotOptimize(p); + + } +} + +BENCHMARK_CAPTURE(BM_LowerBound, random_int32, getRandomIntegerInputs) + ->Arg(TestNumInputs) // Small int32_t vector + ->Arg(TestNumInputs * TestNumInputs); // Big int32_t vector + +BENCHMARK_CAPTURE(BM_LowerBound, random_int64, getRandomIntegerInputs) + ->Arg(TestNumInputs); // Small int64_t vector. Should also represent pointers. + +BENCHMARK_CAPTURE(BM_LowerBound, random_strings, getRandomStringInputs) + ->Arg(TestNumInputs); // Small string vector. What happens if the comparison is not very cheap. + +BENCHMARK(BM_LowerBoundSeanParent); + +template +void BM_EqualRange(benchmark::State& st, GenInputs gen) +{ + do_binary_search_benchmark(st, gen, [](auto f, auto l, const auto& v) { + return std::equal_range(f, l, v); + }); +} + +BENCHMARK_CAPTURE(BM_EqualRange, random_int32, getRandomIntegerInputs) + ->Arg(TestNumInputs) // Small int32_t vector + ->Arg(TestNumInputs * TestNumInputs); // Big int32_t vector + +BENCHMARK_CAPTURE(BM_EqualRange, random_int64, getRandomIntegerInputs) + ->Arg(TestNumInputs); // Small int64_t vector. Should also represent pointers. + +BENCHMARK_CAPTURE(BM_EqualRange, random_strings, getRandomStringInputs) + ->Arg(TestNumInputs); // Small string vector. What happens if the comparison is not very cheap. + BENCHMARK_MAIN(); Index: include/algorithm =================================================================== --- include/algorithm +++ include/algorithm @@ -750,6 +750,32 @@ bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);} }; +// Perform division by two quickly for positive integers (llvm.org/PR39129) + +template +_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR +typename enable_if +< + is_integral<_Integral>::value, + _Integral +>::type +__half_positive(_Integral __value) +{ + return static_cast<_Integral>(static_cast::type>(__value) / 2); +} + +template +_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR +typename enable_if +< + !is_integral<_Tp>::value, + _Tp +>::type +__half_positive(_Tp __value) +{ + return __value / 2; +} + #ifdef _LIBCPP_DEBUG template @@ -3202,7 +3228,7 @@ difference_type __len = _VSTD::distance(__first, __last); while (__len != 0) { - difference_type __l2 = __len / 2; + difference_type __l2 = _VSTD::__half_positive(__len); _ForwardIterator __m = __first; _VSTD::advance(__m, __l2); if (__pred(*__m)) @@ -4069,7 +4095,7 @@ difference_type __len = _VSTD::distance(__first, __last); while (__len != 0) { - difference_type __l2 = __len / 2; + difference_type __l2 = _VSTD::__half_positive(__len); _ForwardIterator __m = __first; _VSTD::advance(__m, __l2); if (__comp(*__m, __value_)) @@ -4111,7 +4137,7 @@ difference_type __len = _VSTD::distance(__first, __last); while (__len != 0) { - difference_type __l2 = __len / 2; + difference_type __l2 = _VSTD::__half_positive(__len); _ForwardIterator __m = __first; _VSTD::advance(__m, __l2); if (__comp(__value_, *__m)) @@ -4153,7 +4179,7 @@ difference_type __len = _VSTD::distance(__first, __last); while (__len != 0) { - difference_type __l2 = __len / 2; + difference_type __l2 = _VSTD::__half_positive(__len); _ForwardIterator __m = __first; _VSTD::advance(__m, __l2); if (__comp(*__m, __value_))