-
-
Save ignisf/437cfdcbb4a1da5b142efc90408eeafd to your computer and use it in GitHub Desktop.
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 'benchmark/ips' | |
require 'set' | |
numbers = (1..10000).to_a | |
numbers_as_string = "," + (1..10000).to_a.join(",") + "," | |
numbers_as_set = Set.new(numbers) | |
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.report("Set") do |i| | |
while i > 0 | |
numbers_as_set.include?(5000) | |
i -= 1 | |
end | |
end | |
x.compare! | |
end |
Author
ignisf
commented
May 4, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment