Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created February 3, 2009 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save igrigorik/57306 to your computer and use it in GitHub Desktop.
Save igrigorik/57306 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'benchmark'
require 'facets/dictionary'
require 'rbtree'
n = 1000000
Benchmark.bm do |x|
@h = Hash.new
x.report("Hash insert") { n.times do @h[rand(n)] = 1 end }
x.report("Hash access") { n.times do @h[rand(n)] end }
@d = Dictionary.new
x.report("Dict insert") { n.times do @d[rand(n)] = 1 end }
x.report("Dict access") { n.times do @d[rand(n)] end }
@r = RBTree.new
x.report("RBTree insert") { n.times do @r[rand] = rand end }
x.report("RBTree access") { n.times do @r[rand] end }
end
# Ruby 1.8.7
# user system total real
# Hash insert 0.350000 0.610000 0.960000 ( 0.990365)
# Hash access 0.770000 0.010000 0.780000 ( 0.801897)
# Dict insert 1.000000 1.240000 2.240000 ( 2.333807)
# Dict access 1.100000 0.020000 1.120000 ( 1.304059)
# RBTree insert 4.820000 1.000000 5.820000 ( 6.293545)
# RBTree access 5.180000 0.110000 5.290000 ( 6.060176)
#
# Ruby 1.9.1
# user system total real
# Hash insert 0.650000 0.150000 0.800000 ( 0.828039)
# Hash access 0.650000 0.000000 0.650000 ( 0.683463)
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment