Skip to content

Instantly share code, notes, and snippets.

@hassox
Forked from jnicklas/benchmark.rb
Created August 30, 2012 23:28
Show Gist options
  • Select an option

  • Save hassox/3544925 to your computer and use it in GitHub Desktop.

Select an option

Save hassox/3544925 to your computer and use it in GitHub Desktop.

Revisions

  1. Daniel Neighman revised this gist Aug 30, 2012. 2 changed files with 10 additions and 17 deletions.
    19 changes: 6 additions & 13 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,12 @@
    require "benchmark"

    string_hash = {}
    symbol_hash = {}
    hash = {'key' => 1, :key => 2}

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

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

    Benchmark.bm do |x|
    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 } }
    x.report("strings") { n.times { hash['key'] } }
    x.report("symbols") { n.times { hash[:key] } }
    x.report("strings, set") { n.times { hash['key'] = 1 } }
    x.report("symbols, set") { n.times { hash[:key] = 1 } }
    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.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)
    strings 1.040000 0.010000 1.050000 ( 1.044701)
    symbols 0.460000 0.000000 0.460000 ( 0.461030)
    strings, set 1.250000 0.000000 1.250000 ( 1.253017)
    symbols, set 0.690000 0.000000 0.690000 ( 0.689607)
  2. @jnicklas 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)
  3. @jnicklas 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)