Last active
August 29, 2015 14:10
-
-
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
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
#!/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 |
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
./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