Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Array vs Set benchmark
require 'benchmark'
require 'set'
array = File.readlines('dutch.bin') # 10k lines
hash = Hash[*array] # 5k keys => 5k values
set = array.to_set # 10k entries
line = array[-1] # Last line
Benchmark.bmbm do |x|
x.report("Array.include?") { 10_000.times { array.include?(line)} }
x.report("Hash.include?") { 10_000.times { hash.include?(line)} }
x.report("Set.include?") { 10_000.times { set.include?(line)} }
end
@evaneykelen

This comment has been minimized.

Copy link
Owner Author

evaneykelen commented Aug 25, 2019

Rehearsal --------------------------------------------------
Array.include?   1.064075   0.003050   1.067125 (  1.070542)
Hash.include?    0.001348   0.000022   0.001370 (  0.001382)
Set.include?     0.001442   0.000039   0.001481 (  0.001522)
----------------------------------------- total: 1.069976sec

                     user     system      total        real
Array.include?   1.088570   0.004247   1.092817 (  1.097795)
Hash.include?    0.001268   0.000002   0.001270 (  0.001265)
Set.include?     0.001467   0.000001   0.001468 (  0.001465)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.