Skip to content

Instantly share code, notes, and snippets.

@jnicklas
Created August 30, 2012 23:04
Show Gist options
  • Select an option

  • Save jnicklas/3544217 to your computer and use it in GitHub Desktop.

Select an option

Save jnicklas/3544217 to your computer and use it in GitHub Desktop.

Revisions

  1. jnicklas revised this gist Aug 30, 2012. 2 changed files with 10 additions and 10 deletions.
    12 changes: 6 additions & 6 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    require "benchmark"

    string_hash = {}
    @@ -9,11 +8,12 @@
    symbol_hash[:"key_#{i}"] = i
    end

    accessors = Array.new(100000) { rand(50000) }
    string_accessors = Array.new(100000) { "key_#{rand(50000)}" }
    symbol_accessors = Array.new(100000) { :"key_#{rand(50000)}" }

    Benchmark.bm do |x|
    x.report("strings") { accessors.each { |a| string_hash[a] } }
    x.report("symbols") { accessors.each { |a| symbol_hash[a] } }
    x.report("strings, set") { accessors.each { |a| string_hash[a] = a } }
    x.report("symbols, set") { accessors.each { |a| symbol_hash[a] = a } }
    x.report("strings") { string_accessors.each { |a| string_hash[a] } }
    x.report("symbols") { symbol_accessors.each { |a| symbol_hash[a] } }
    x.report("strings, set") { string_accessors.each { |a| string_hash[a] = a } }
    x.report("symbols, set") { symbol_accessors.each { |a| symbol_hash[a] = a } }
    end
    8 changes: 4 additions & 4 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    user system total real
    strings 0.020000 0.000000 0.020000 ( 0.018910)
    symbols 0.020000 0.000000 0.020000 ( 0.018612)
    strings, set 0.030000 0.000000 0.030000 ( 0.029713)
    symbols, set 0.030000 0.000000 0.030000 ( 0.031306)
    strings 0.030000 0.000000 0.030000 ( 0.031394)
    symbols 0.010000 0.000000 0.010000 ( 0.019276)
    strings, set 0.040000 0.000000 0.040000 ( 0.036649)
    symbols, set 0.030000 0.000000 0.030000 ( 0.024406)
  2. jnicklas created this gist Aug 30, 2012.
    19 changes: 19 additions & 0 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@

    require "benchmark"

    string_hash = {}
    symbol_hash = {}

    (0..50000).map do |i|
    string_hash["key_#{i}"] = i
    symbol_hash[:"key_#{i}"] = i
    end

    accessors = Array.new(100000) { rand(50000) }

    Benchmark.bm do |x|
    x.report("strings") { accessors.each { |a| string_hash[a] } }
    x.report("symbols") { accessors.each { |a| symbol_hash[a] } }
    x.report("strings, set") { accessors.each { |a| string_hash[a] = a } }
    x.report("symbols, set") { accessors.each { |a| symbol_hash[a] = a } }
    end
    5 changes: 5 additions & 0 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    user system total real
    strings 0.020000 0.000000 0.020000 ( 0.018910)
    symbols 0.020000 0.000000 0.020000 ( 0.018612)
    strings, set 0.030000 0.000000 0.030000 ( 0.029713)
    symbols, set 0.030000 0.000000 0.030000 ( 0.031306)