Skip to content

Instantly share code, notes, and snippets.

@jabley
Created July 15, 2009 17:55
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 jabley/147861 to your computer and use it in GitHub Desktop.
Save jabley/147861 to your computer and use it in GitHub Desktop.
$ cat bench-hash-creation.rb
require 'benchmark'
n = 1_000_000
Benchmark.bm do |x|
x.report('arrays') {
k = []
v = []
for i in 1..n do
k << i
v << i
end
Hash[*k.zip(v).flatten]
}
x.report('direct') {
h = {}
for i in 1..n do
h[i] = i
end
}
end
$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
$ ruby bench-hash-creation.rb
user system total real
arrays 2.740000 0.350000 3.090000 ( 3.389127)
direct 1.040000 0.290000 1.330000 ( 1.430653)
$ jruby -v
jruby 1.3.1 (ruby 1.8.6p287) (2009-06-15 2fd6c3d) (Java HotSpot(TM) Client VM 1.6.0_14) [i386-java]
$ jruby --server bench-hash-creation.rb
user system total real
arrays861.593000 0.000000 861.593000 (861.506000)
direct 4.180000 0.000000 4.180000 ( 4.180000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment