Created
July 19, 2020 22:01
-
-
Save richdrich/a0033e50a5b7789f1ff534ec2b5fea11 to your computer and use it in GitHub Desktop.
bsearch_index vs index
This file contains 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 '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