Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Created May 6, 2021 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamSaffron/a96e5abd43bc044a2055b79c01e4cd44 to your computer and use it in GitHub Desktop.
Save SamSaffron/a96e5abd43bc044a2055b79c01e4cd44 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
numbers = (1..10000).to_a
numbers_as_string = "," + (1..10000).to_a.join(",") + ","
numbers_as_hash = {}
(1..10000).each {|n| numbers_as_hash[n] = true }
serialized_numbers = Marshal.dump(numbers)
serialized_string = Marshal.dump(numbers_as_string)
serialized_hash = Marshal.dump(numbers_as_hash)
Benchmark.ips do |x|
x.report("binary search numbers") do |i|
while i > 0
numbers = Marshal.load(serialized_numbers)
numbers.bsearch { |x| x == 77 }
i -= 1
end
end
x.report("numbers") do |i|
while i > 0
numbers = Marshal.load(serialized_numbers)
numbers.include?(5000)
i -= 1
end
end
x.report("string") do |i|
while i > 0
numbers_as_string = Marshal.load(serialized_string)
numbers_as_string.include?(",5000,")
i -= 1
end
end
x.report("hash") do |i|
while i > 0
numbers_as_hash = Marshal.load(serialized_hash)
numbers_as_hash[5000]
i -= 1
end
end
x.compare!
end
#Comparison:
# string: 168019.7 i/s
#binary search numbers: 5586.9 i/s - 30.07x (± 0.00) slower
# numbers: 5061.3 i/s - 33.20x (± 0.00) slower
# hash: 2558.9 i/s - 65.66x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment