diff options
author | Kouhei Yanagita <[email protected]> | 2023-09-16 12:10:09 +0900 |
---|---|---|
committer | GitHub <[email protected]> | 2023-09-16 12:10:09 +0900 |
commit | 7d08dbd015a16c27f2d9c77751e80fa6efba2d7a (patch) | |
tree | f40369e5c50acc88026c5221695443342fb5c49c | |
parent | 67dedf8cf634843488a477e53b9995b63e9aa291 (diff) |
Optimize Range#bsearch for beginless/endless ranges
On Range#bsearch for endless ranges, we try positions at `begin + 2**i` (i = 0, 1, 2, ...)
to find a point that satisfies a given condition.
Subsequently, we perform binary searching with the interval `[begin, begin + 2**n]`.
However, the interval `[begin + 2**(n-1), begin + 2**n]` is sufficient for binary search
because `begin + 2**(n-1)` does not satisfy the condition.
The same applies to beginless ranges.
-rw-r--r-- | benchmark/range_bsearch.yml | 9 | ||||
-rw-r--r-- | range.c | 2 |
2 files changed, 11 insertions, 0 deletions
diff --git a/benchmark/range_bsearch.yml b/benchmark/range_bsearch.yml new file mode 100644 index 0000000000..9a7388ab04 --- /dev/null +++ b/benchmark/range_bsearch.yml @@ -0,0 +1,9 @@ +prelude: | + r = (1..) + +benchmark: + '10**1': r.bsearch { |x| x >= 10 } + '10**2': r.bsearch { |x| x >= 100 } + '10**3': r.bsearch { |x| x >= 1000 } + '10**4': r.bsearch { |x| x >= 10000 } + '10**5': r.bsearch { |x| x >= 100000 } @@ -758,6 +758,7 @@ range_bsearch(VALUE range) return bsearch_integer_range(beg, mid, 0); } diff = rb_funcall(diff, '*', 1, LONG2FIX(2)); + beg = mid; } } else if (NIL_P(beg) && is_integer_p(end)) { @@ -770,6 +771,7 @@ range_bsearch(VALUE range) return bsearch_integer_range(mid, end, 0); } diff = rb_funcall(diff, '*', 1, LONG2FIX(2)); + end = mid; } } else { |