Created
May 8, 2014 14:26
-
-
Save halida/aeab1b39a5b10990e346 to your computer and use it in GitHub Desktop.
ruby bsearch vs index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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