Skip to content

Commit 72df628

Browse files
committed
update benchmarks
1 parent fd0e8c6 commit 72df628

File tree

2 files changed

+93
-58
lines changed

2 files changed

+93
-58
lines changed

README.md

+56-38
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,71 @@ BinarySearch is a Ruby gem that implements a self-balancing binary search tree u
1515

1616
## Benchmark results:
1717
```bash
18+
ruby 3.3.3 (2024-06-12 revision f1c7b6f435) +YJIT [x86_64-linux]
1819
Benchmarking with 10,000 elements:
19-
user system total real
20-
Array insert: 0.000455 0.000000 0.000455 ( 0.000453)
21-
BinarySearch insert: 0.010808 0.000735 0.011543 ( 0.011955)
22-
NArray insert: 0.000023 0.000007 0.000030 ( 0.000030)
23-
Array search: 0.149230 0.000000 0.149230 ( 0.149390)
24-
BinarySearch search: 0.001719 0.000000 0.001719 ( 0.001720)
25-
NArray search: 0.210021 0.000861 0.210882 ( 0.210903)
26-
Array delete: 0.466095 0.000000 0.466095 ( 0.466142)
27-
BinarySearch delete: 0.006978 0.000986 0.007964 ( 0.007965)
28-
NArray delete: 0.344920 0.045966 0.390886 ( 0.391025)
20+
user system total real
21+
Array#<< (append): 0.000318 0.000083 0.000401 ( 0.000398)
22+
BinarySearch GEM#insert: 0.012594 0.000000 0.012594 ( 0.012609)
23+
Numo::NArray.new + .seq: 0.000042 0.000000 0.000042 ( 0.000042)
24+
SortedSet#add: 0.008358 0.001024 0.009382 ( 0.009516)
25+
Array#include?: 0.150862 0.000000 0.150862 ( 0.150930)
26+
BinarySearch GEM#include?: 0.001820 0.000000 0.001820 ( 0.001821)
27+
Numo::NArray#eq + .any?: 0.215213 0.000000 0.215213 ( 0.215412)
28+
Array#bsearch (std lib): 0.004166 0.000000 0.004166 ( 0.004166)
29+
Array#delete: 0.471802 0.000000 0.471802 ( 0.471888)
30+
BinarySearch GEM#delete: 0.007105 0.000991 0.008096 ( 0.008099)
31+
Numo::NArray delete (mask): 0.363630 0.039971 0.403601 ( 0.403405)
32+
SortedSet#delete: 0.008109 0.000005 0.008114 ( 0.008119)
2933
Insertion:
30-
Array is 15.1x slower than the fastest
31-
Binary is 398.93x slower than the fastest
32-
Narray is the fastest
34+
Array#<< (append): is 9.55x slower than the fastest
35+
BinarySearch GEM#insert: is 302.16x slower than the fastest
36+
Numo::NArray.new + .seq: is the fastest
37+
SortedSet#add: is 228.03x slower than the fastest
38+
3339
Search:
34-
Array is 86.85x slower than the fastest
35-
Binary is the fastest
36-
Narray is 122.61x slower than the fastest
40+
Array#include?: is 82.9x slower than the fastest
41+
BinarySearch GEM#include?: is the fastest
42+
Numo::NArray#eq + .any?: is 118.32x slower than the fastest
43+
Array#bsearch (std lib): is 2.29x slower than the fastest
44+
3745
Deletion:
38-
Array is 58.52x slower than the fastest
39-
Binary is the fastest
40-
Narray is 49.09x slower than the fastest
46+
Array#delete: is 58.26x slower than the fastest
47+
BinarySearch GEM#delete: is the fastest
48+
Numo::NArray delete (mask): is 49.81x slower than the fastest
49+
SortedSet#delete: is 1.0x slower than the fastest
50+
4151
4252
Benchmarking with 100,000 elements:
43-
user system total real
44-
Array insert: 0.003449 0.000000 0.003449 ( 0.003449)
45-
BinarySearch insert: 0.089313 0.000000 0.089313 ( 0.089320)
46-
NArray insert: 0.000095 0.000000 0.000095 ( 0.000095)
47-
Array search: 15.813410 0.002981 15.816391 ( 15.815585)
48-
BinarySearch search: 0.013462 0.000002 0.013464 ( 0.013466)
49-
NArray search: 18.734754 0.001999 18.736753 ( 18.737876)
50-
Array delete: 47.327969 0.000001 47.327970 ( 47.332518)
51-
BinarySearch delete: 0.042072 0.000001 0.042073 ( 0.042075)
52-
NArray delete: 29.808121 3.051711 32.859832 ( 32.851348)
53+
user system total real
54+
Array#<< (append): 0.003539 0.000022 0.003561 ( 0.003571)
55+
BinarySearch GEM#insert: 0.085730 0.004032 0.089762 ( 0.089782)
56+
Numo::NArray.new + .seq: 0.000109 0.000004 0.000113 ( 0.000095)
57+
SortedSet#add: 0.060403 0.000000 0.060403 ( 0.060411)
58+
Array#include?: 16.131343 0.002956 16.134299 ( 16.133787)
59+
BinarySearch GEM#include?: 0.013607 0.000002 0.013609 ( 0.013611)
60+
Numo::NArray#eq + .any?: 18.948453 0.007996 18.956449 ( 18.957555)
61+
Array#bsearch (std lib): 0.048986 0.000000 0.048986 ( 0.048997)
62+
Array#delete: 47.501618 0.001000 47.502618 ( 47.508824)
63+
BinarySearch GEM#delete: 0.043180 0.000001 0.043181 ( 0.043194)
64+
Numo::NArray delete (mask): 29.937312 2.154682 32.091994 ( 32.080060)
65+
SortedSet#delete: 0.100403 0.000000 0.100403 ( 0.100697)
5366
Insertion:
54-
Array is 36.49x slower than the fastest
55-
Binary is 944.99x slower than the fastest
56-
Narray is the fastest
67+
Array#<< (append): is 37.69x slower than the fastest
68+
BinarySearch GEM#insert: is 947.56x slower than the fastest
69+
Numo::NArray.new + .seq: is the fastest
70+
SortedSet#add: is 637.58x slower than the fastest
71+
5772
Search:
58-
Array is 1174.52x slower than the fastest
59-
Binary is the fastest
60-
Narray is 1391.54x slower than the fastest
73+
Array#include?: is 1185.37x slower than the fastest
74+
BinarySearch GEM#include?: is the fastest
75+
Numo::NArray#eq + .any?: is 1392.84x slower than the fastest
76+
Array#bsearch (std lib): is 3.6x slower than the fastest
77+
6178
Deletion:
62-
Array is 1124.94x slower than the fastest
63-
Binary is the fastest
64-
Narray is 780.77x slower than the fastest
79+
Array#delete: is 1099.89x slower than the fastest
80+
BinarySearch GEM#delete: is the fastest
81+
Numo::NArray delete (mask): is 742.7x slower than the fastest
82+
SortedSet#delete: is 2.33x slower than the fastest
6583
```
6684
6785
## Installation 💻

benchmark/vs_native_ruby.rb

+37-20
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,99 @@
33
require 'benchmark'
44
require 'ruby_binary_search'
55
require 'numo/narray'
6+
require 'set'
67

78
def run_benchmark(n)
89
array = []
9-
binary_search = BinarySearch::List.new
10-
narray = Numo::Int32.new(n).seq
10+
binary_search_gem = BinarySearch::List.new
11+
narray = Numo::Int32.new(n)
12+
sorted_array = (0...n).to_a
1113

1214
results = {}
1315

14-
Benchmark.bm(20) do |x|
16+
Benchmark.bm(30) do |x|
1517
# Insertion
16-
results[:array_insert] = x.report('Array insert:') do
18+
results[:array_insert] = x.report('Array#<< (append):') do
1719
n.times { |i| array << i }
1820
end
1921

20-
results[:binary_insert] = x.report('BinarySearch insert:') do
21-
n.times { |i| binary_search.insert(i) }
22+
results[:binary_search_gem_insert] = x.report('BinarySearch GEM#insert:') do
23+
n.times { |i| binary_search_gem.insert(i) }
2224
end
2325

24-
results[:narray_insert] = x.report('NArray insert:') do
25-
Numo::Int32.new(n).seq
26+
results[:narray_insert] = x.report('Numo::NArray.new + .seq:') do
27+
narray = Numo::Int32.new(n).seq
28+
end
29+
30+
results[:sorted_set_insert] = x.report('SortedSet#add:') do
31+
sorted_set = SortedSet.new
32+
n.times { |i| sorted_set.add(i) }
2633
end
2734

2835
# Search
29-
results[:array_search] = x.report('Array search:') do
36+
results[:array_search] = x.report('Array#include?:') do
3037
n.times { |i| array.include?(i) }
3138
end
3239

33-
results[:binary_search] = x.report('BinarySearch search:') do
34-
n.times { |i| binary_search.include?(i) }
40+
results[:binary_search_gem_search] = x.report('BinarySearch GEM#include?:') do
41+
n.times { |i| binary_search_gem.include?(i) }
3542
end
3643

37-
results[:narray_search] = x.report('NArray search:') do
44+
results[:narray_search] = x.report('Numo::NArray#eq + .any?:') do
3845
n.times { |i| narray.eq(i).any? }
3946
end
4047

48+
results[:array_bsearch] = x.report('Array#bsearch (std lib):') do
49+
n.times { |i| sorted_array.bsearch { |x| x >= i } }
50+
end
51+
4152
# Deletion
42-
results[:array_delete] = x.report('Array delete:') do
53+
results[:array_delete] = x.report('Array#delete:') do
4354
n.times { |i| array.delete(i) }
4455
end
4556

46-
results[:binary_delete] = x.report('BinarySearch delete:') do
47-
n.times { |i| binary_search.delete(i) }
57+
results[:binary_search_gem_delete] = x.report('BinarySearch GEM#delete:') do
58+
n.times { |i| binary_search_gem.delete(i) }
4859
end
4960

50-
results[:narray_delete] = x.report('NArray delete:') do
61+
results[:narray_delete] = x.report('Numo::NArray delete (mask):') do
5162
temp_narray = narray.copy
5263
n.times do |i|
5364
mask = temp_narray.ne(i)
5465
temp_narray = temp_narray[mask]
5566
end
5667
end
68+
69+
results[:sorted_set_delete] = x.report('SortedSet#delete:') do
70+
sorted_set = SortedSet.new(0...n)
71+
n.times { |i| sorted_set.delete(i) }
72+
end
5773
end
5874

5975
print_comparison(results)
6076
end
6177

6278
def print_comparison(results)
6379
operations = [
64-
['Insertion', :array_insert, :binary_insert, :narray_insert],
65-
['Search', :array_search, :binary_search, :narray_search],
66-
['Deletion', :array_delete, :binary_delete, :narray_delete]
80+
['Insertion', :array_insert, :binary_search_gem_insert, :narray_insert, :sorted_set_insert],
81+
['Search', :array_search, :binary_search_gem_search, :narray_search, :array_bsearch],
82+
['Deletion', :array_delete, :binary_search_gem_delete, :narray_delete, :sorted_set_delete]
6783
]
6884

6985
operations.each do |op, *keys|
7086
puts "#{op}:"
7187
times = keys.map { |k| results[k].real }
7288
fastest = times.min
7389
keys.zip(times).each do |key, time|
74-
name = key.to_s.split('_').first.capitalize
90+
name = results[key].label.strip
7591
if time == fastest
7692
puts " #{name} is the fastest"
7793
else
7894
times_slower = (time / fastest).round(2)
7995
puts " #{name} is #{times_slower}x slower than the fastest"
8096
end
8197
end
98+
puts
8299
end
83100
end
84101

0 commit comments

Comments
 (0)