Skip to content

Commit

Permalink
Optimized lower_bound/binary_search algorithms.
Browse files Browse the repository at this point in the history
  • Loading branch information
tylov committed Feb 11, 2024
1 parent dbff6ca commit c46d26d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
8 changes: 5 additions & 3 deletions include/stc/algo/quicksort.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,21 @@ static inline void _c_MEMB(_quicksort_ij)(i_type* arr, intptr_t lo, intptr_t hi)

static inline intptr_t // -1 = not found
_c_MEMB(_lower_bound_range)(const i_type* arr, const _m_raw raw, intptr_t first, intptr_t last) {
intptr_t step, count = last - first;
intptr_t count = last - first, step = count/2;

while (count > 0) {
intptr_t it = first;
step = count / 2;
it += step;

const _m_raw rx = i_keyto(i_at(arr, it));
if (i_less((&rx), (&raw))) {
first = ++it;
count -= step + 1;
} else
step = count*3/4;
} else {
count = step;
step = count/4;
}
}
return first == last ? -1 : first;
}
Expand Down
22 changes: 12 additions & 10 deletions misc/benchmarks/plotbench/run_gcc.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
g++ -DNDEBUG -I../../../include -O3 -o deq_benchmark deq_benchmark.cpp
g++ -DNDEBUG -I../../../include -O3 -o list_benchmark list_benchmark.cpp
g++ -DNDEBUG -I../../../include -O3 -o hmap_benchmark hmap_benchmark.cpp
g++ -DNDEBUG -I../../../include -O3 -o smap_benchmark smap_benchmark.cpp
g++ -DNDEBUG -I../../../include -O3 -o vec_benchmark vec_benchmark.cpp
exe=''
if [ "$OS" = "Windows_NT" ] ; then exe=".exe" ; fi
g++ -DNDEBUG -I../../../include -O3 -o deq_benchmark$exe deq_benchmark.cpp
g++ -DNDEBUG -I../../../include -O3 -o list_benchmark$exe list_benchmark.cpp
g++ -DNDEBUG -I../../../include -O3 -o hmap_benchmark$exe hmap_benchmark.cpp
g++ -DNDEBUG -I../../../include -O3 -o smap_benchmark$exe smap_benchmark.cpp
g++ -DNDEBUG -I../../../include -O3 -o vec_benchmark$exe vec_benchmark.cpp

c='Mingw-g++-13.1.0'
./deq_benchmark $c
./list_benchmark $c
./hmap_benchmark $c
./smap_benchmark $c
./vec_benchmark $c
./deq_benchmark$exe $c
./list_benchmark$exe $c
./hmap_benchmark$exe $c
./smap_benchmark$exe $c
./vec_benchmark$exe $c
2 changes: 1 addition & 1 deletion misc/benchmarks/various/binsearch_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

int main(int argc, char const *argv[])
{
intptr_t N = 10000000;
intptr_t N = 5000000;
unsigned mask = (1 << 23) - 1;
ivec v = {0};
c_forrange (i, N)
Expand Down

0 comments on commit c46d26d

Please sign in to comment.