Skip to content

Instantly share code, notes, and snippets.

@halida
Created May 8, 2014 14:26
Show Gist options
  • Save halida/aeab1b39a5b10990e346 to your computer and use it in GitHub Desktop.
Save halida/aeab1b39a5b10990e346 to your computer and use it in GitHub Desktop.
ruby bsearch vs index
require 'benchmark'
a = (1..1000).to_a
lookup = 100000.times.map{(rand*1000).to_i}
puts "for index"
puts Benchmark.measure {
lookup.each{ |i| a.index(i) }
}
puts "for bsearch"
puts Benchmark.measure {
lookup.each{ |i| a.bsearch{|v| v >= i} }
}
$ ruby bsearch.rb
for index
6.080000 0.010000 6.090000 ( 6.084225)
for bsearch
0.220000 0.000000 0.220000 ( 0.218332)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment