Skip to content

Instantly share code, notes, and snippets.

@maasha
Last active August 29, 2015 14:10
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 maasha/03b63404665c63974c4f to your computer and use it in GitHub Desktop.
Save maasha/03b63404665c63974c4f to your computer and use it in GitHub Desktop.
Benchmark of Set vs Ruby Hash vs Google Dense Hash vs Google Sparse Hash
#!/usr/bin/env ruby
require 'set'
require 'google_hash'
require 'benchmark'
set = Set.new
hash = Hash.new(0)
ghashd = GoogleHashDenseIntToInt.new
ghashs = GoogleHashSparseIntToInt.new
max = 200_000
n = 10_000_000
Benchmark.bm() do |x|
x.report("Set") { n.times { set.add(rand(max)) } }
x.report("Ruby Hash") { n.times { hash[rand(max)] = 1 } }
x.report("Google Hash Dense") { n.times { ghashd[rand(max)] = 1 } }
x.report("Google Hash Sparse") { n.times { ghashs[rand(max)] = 1 } }
end
./benchmark.rb
user system total real
Set 6.490000 0.020000 6.510000 ( 6.595643)
Ruby Hash 5.950000 0.030000 5.980000 ( 6.066545)
Google Hash Dense 5.830000 0.010000 5.840000 ( 5.904371)
Google Hash Sparse 9.490000 0.010000 9.500000 ( 9.531793)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment