Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Created May 4, 2021 22:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SamSaffron/df1a982f065b85c1861905c5a3171448 to your computer and use it in GitHub Desktop.
Save SamSaffron/df1a982f065b85c1861905c5a3171448 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
numbers = (1..10000).to_a
numbers_as_string = "," + (1..10000).to_a.join(",") + ","
Benchmark.ips do |x|
x.report("binary search numbers") do |i|
while i > 0
numbers.bsearch { |x| x == 77 }
i -= 1
end
end
x.report("numbers") do |i|
while i > 0
numbers.include?(5000)
i -= 1
end
end
x.report("string") do |i|
while i > 0
numbers_as_string.include?(",5000,")
i -= 1
end
end
x.compare!
end
@SamSaffron
Copy link
Author

binary search numbers:  3136476.0 i/s
              string:   387179.8 i/s - 8.10x  (± 0.00) slower
             numbers:    46601.0 i/s - 67.30x  (± 0.00) slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment