Skip to content

Instantly share code, notes, and snippets.

@richdrich
Created July 19, 2020 22:01
Show Gist options
  • Save richdrich/a0033e50a5b7789f1ff534ec2b5fea11 to your computer and use it in GitHub Desktop.
Save richdrich/a0033e50a5b7789f1ff534ec2b5fea11 to your computer and use it in GitHub Desktop.
bsearch_index vs index
require 'date'
NUM_ENTRIES = 100000
NUM_OPS=1000
a = Array.new(NUM_ENTRIES)
tstart = DateTime.now
a.fill {|n| tstart + n - NUM_ENTRIES }
t = DateTime.now
NUM_OPS.times do |n|
r = rand(NUM_ENTRIES)
wanted = tstart + r - NUM_ENTRIES
e = a.bsearch_index {|v| v >= wanted }
raise "#{a[e]} != #{wanted}" unless a[e] == wanted
end
puts "#{(DateTime.now - t).to_f * 1000 * 86400 / NUM_OPS}ms per search"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment