Skip to content

Instantly share code, notes, and snippets.

@tiagoamaro
Last active September 2, 2024 15:36
Show Gist options
  • Select an option

  • Save tiagoamaro/c82a27aceedfc901b081 to your computer and use it in GitHub Desktop.

Select an option

Save tiagoamaro/c82a27aceedfc901b081 to your computer and use it in GitHub Desktop.

Revisions

  1. tiagoamaro revised this gist Sep 2, 2024. 1 changed file with 56 additions and 1 deletion.
    57 changes: 56 additions & 1 deletion results_with_7.2.1.txt
    Original file line number Diff line number Diff line change
    @@ -1 +1,56 @@
    TODO
    ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
    Warming up --------------------------------------
    Common Hash#fetch access
    1.931M i/100ms
    Common Hash#[] access
    2.664M i/100ms
    HashWithIndifferentAccess#fetch Symbol
    435.730k i/100ms
    HashWithIndifferentAccess#fetch String
    466.802k i/100ms
    HashWithIndifferentAccess #[] Symbol
    857.927k i/100ms
    HashWithIndifferentAccess #[] String
    763.909k i/100ms
    Hash#with_indifferent_access #fetch Symbol
    125.448k i/100ms
    Hash#with_indifferent_access #fetch String
    121.645k i/100ms
    Hash#with_indifferent_access #[] Symbol
    139.318k i/100ms
    Hash#with_indifferent_access #[] String
    133.811k i/100ms
    Calculating -------------------------------------
    Common Hash#fetch access
    21.725M (± 4.0%) i/s - 110.067M in 5.076061s
    Common Hash#[] access
    29.786M (± 5.6%) i/s - 149.172M in 5.027325s
    HashWithIndifferentAccess#fetch Symbol
    5.031M (± 6.4%) i/s - 25.272M in 5.048376s
    HashWithIndifferentAccess#fetch String
    4.615M (± 1.8%) i/s - 23.340M in 5.058678s
    HashWithIndifferentAccess #[] Symbol
    8.453M (± 1.4%) i/s - 42.896M in 5.075377s
    HashWithIndifferentAccess #[] String
    7.481M (± 0.9%) i/s - 37.432M in 5.004266s
    Hash#with_indifferent_access #fetch Symbol
    1.224M (± 2.0%) i/s - 6.147M in 5.025362s
    Hash#with_indifferent_access #fetch String
    1.192M (± 3.0%) i/s - 5.961M in 5.004910s
    Hash#with_indifferent_access #[] Symbol
    1.388M (± 1.4%) i/s - 6.966M in 5.019358s
    Hash#with_indifferent_access #[] String
    1.140M (±28.7%) i/s - 4.951M in 5.102470s

    Comparison:
    Common Hash#[] access: 29785530.7 i/s
    Common Hash#fetch access: 21725030.4 i/s - 1.37x slower
    HashWithIndifferentAccess #[] Symbol: 8453452.2 i/s - 3.52x slower
    HashWithIndifferentAccess #[] String: 7480508.2 i/s - 3.98x slower
    HashWithIndifferentAccess#fetch Symbol: 5031316.4 i/s - 5.92x slower
    HashWithIndifferentAccess#fetch String: 4615393.0 i/s - 6.45x slower
    Hash#with_indifferent_access #[] Symbol: 1388100.2 i/s - 21.46x slower
    Hash#with_indifferent_access #fetch Symbol: 1223707.7 i/s - 24.34x slower
    Hash#with_indifferent_access #fetch String: 1192117.0 i/s - 24.99x slower
    Hash#with_indifferent_access #[] String: 1139719.0 i/s - 26.13x slower

  2. tiagoamaro revised this gist Sep 2, 2024. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions hash_with_indifferent_access_benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@

    # gem 'activesupport', '=3.2.22'
    # gem 'activesupport', '=4.2.5'
    # gem 'activesupport', '=7.2.1'

    require 'active_support/all'
    require 'benchmark/ips'
    1 change: 1 addition & 0 deletions results_with_7.2.1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    TODO
  3. tiagoamaro revised this gist Dec 4, 2015. 4 changed files with 117 additions and 55 deletions.
    7 changes: 7 additions & 0 deletions hash_with_indifferent_access_benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,10 @@
    #####################
    # Ruby Version: 2.1.6
    #####################

    # gem 'activesupport', '=3.2.22'
    # gem 'activesupport', '=4.2.5'

    require 'active_support/all'
    require 'benchmark/ips'

    55 changes: 0 additions & 55 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -1,55 +0,0 @@
    ▶ ruby hash_with_indifferent_access_benchmark.rb
    Calculating -------------------------------------
    Common Hash#fetch access
    49.229k i/100ms
    Common Hash#[] access
    51.376k i/100ms
    HashWithIndifferentAccess#fetch Symbol
    41.975k i/100ms
    HashWithIndifferentAccess#fetch String
    41.612k i/100ms
    HashWithIndifferentAccess #[] Symbol
    38.397k i/100ms
    HashWithIndifferentAccess #[] String
    47.435k i/100ms
    Hash#with_indifferent_access #fetch Symbol
    23.180k i/100ms
    Hash#with_indifferent_access #fetch String
    23.231k i/100ms
    Hash#with_indifferent_access #[] Symbol
    21.770k i/100ms
    Hash#with_indifferent_access #[] String
    23.082k i/100ms
    -------------------------------------------------
    Common Hash#fetch access
    5.984M (± 8.7%) i/s - 29.636M
    Common Hash#[] access
    8.188M (± 8.0%) i/s - 40.536M
    HashWithIndifferentAccess#fetch Symbol
    2.035M (± 7.1%) i/s - 10.116M
    HashWithIndifferentAccess#fetch String
    2.026M (± 7.6%) i/s - 10.070M
    HashWithIndifferentAccess #[] Symbol
    1.424M (± 6.8%) i/s - 7.103M
    HashWithIndifferentAccess #[] String
    3.740M (± 9.9%) i/s - 18.500M
    Hash#with_indifferent_access #fetch Symbol
    408.045k (± 7.6%) i/s - 2.040M
    Hash#with_indifferent_access #fetch String
    438.536k (± 6.3%) i/s - 2.184M
    Hash#with_indifferent_access #[] Symbol
    393.421k (± 7.1%) i/s - 1.959M
    Hash#with_indifferent_access #[] String
    458.767k (± 6.5%) i/s - 2.285M

    Comparison:
    Common Hash#[] access: 8187909.8 i/s
    Common Hash#fetch access: 5983764.2 i/s - 1.37x slower
    HashWithIndifferentAccess #[] String: 3740278.7 i/s - 2.19x slower
    HashWithIndifferentAccess#fetch Symbol: 2034999.4 i/s - 4.02x slower
    HashWithIndifferentAccess#fetch String: 2026388.5 i/s - 4.04x slower
    HashWithIndifferentAccess #[] Symbol: 1424228.9 i/s - 5.75x slower
    Hash#with_indifferent_access #[] String: 458767.5 i/s - 17.85x slower
    Hash#with_indifferent_access #fetch String: 438536.1 i/s - 18.67x slower
    Hash#with_indifferent_access #fetch Symbol: 408044.9 i/s - 20.07x slower
    Hash#with_indifferent_access #[] Symbol: 393421.4 i/s - 20.81x slower
    55 changes: 55 additions & 0 deletions results_with_3.2.22.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    ▶ ruby hash_with_indifferent_access_benchmark.rb
    Calculating -------------------------------------
    Common Hash#fetch access
    48.573k i/100ms
    Common Hash#[] access
    46.887k i/100ms
    HashWithIndifferentAccess#fetch Symbol
    41.125k i/100ms
    HashWithIndifferentAccess#fetch String
    41.433k i/100ms
    HashWithIndifferentAccess #[] Symbol
    35.580k i/100ms
    HashWithIndifferentAccess #[] String
    46.442k i/100ms
    Hash#with_indifferent_access #fetch Symbol
    22.616k i/100ms
    Hash#with_indifferent_access #fetch String
    21.547k i/100ms
    Hash#with_indifferent_access #[] Symbol
    19.568k i/100ms
    Hash#with_indifferent_access #[] String
    21.981k i/100ms
    -------------------------------------------------
    Common Hash#fetch access
    5.751M (±11.7%) i/s - 28.318M
    Common Hash#[] access
    7.998M (±12.2%) i/s - 39.244M
    HashWithIndifferentAccess#fetch Symbol
    2.086M (± 8.4%) i/s - 10.364M
    HashWithIndifferentAccess#fetch String
    2.131M (± 6.5%) i/s - 10.607M
    HashWithIndifferentAccess #[] Symbol
    1.478M (± 8.2%) i/s - 7.365M
    HashWithIndifferentAccess #[] String
    3.846M (± 9.6%) i/s - 19.041M
    Hash#with_indifferent_access #fetch Symbol
    436.058k (± 7.9%) i/s - 2.171M
    Hash#with_indifferent_access #fetch String
    435.556k (± 7.1%) i/s - 2.176M
    Hash#with_indifferent_access #[] Symbol
    407.470k (± 7.2%) i/s - 2.035M
    Hash#with_indifferent_access #[] String
    503.255k (± 7.2%) i/s - 2.506M

    Comparison:
    Common Hash#[] access: 7997771.0 i/s
    Common Hash#fetch access: 5750854.4 i/s - 1.39x slower
    HashWithIndifferentAccess #[] String: 3845945.3 i/s - 2.08x slower
    HashWithIndifferentAccess#fetch String: 2130717.4 i/s - 3.75x slower
    HashWithIndifferentAccess#fetch Symbol: 2085604.3 i/s - 3.83x slower
    HashWithIndifferentAccess #[] Symbol: 1478029.7 i/s - 5.41x slower
    Hash#with_indifferent_access #[] String: 503255.0 i/s - 15.89x slower
    Hash#with_indifferent_access #fetch Symbol: 436057.9 i/s - 18.34x slower
    Hash#with_indifferent_access #fetch String: 435555.6 i/s - 18.36x slower
    Hash#with_indifferent_access #[] Symbol: 407469.7 i/s - 19.63x slower
    55 changes: 55 additions & 0 deletions results_with_4.2.5.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    ▶ ruby hash_with_indifferent_access_benchmark.rb
    Calculating -------------------------------------
    Common Hash#fetch access
    47.253k i/100ms
    Common Hash#[] access
    50.406k i/100ms
    HashWithIndifferentAccess#fetch Symbol
    39.602k i/100ms
    HashWithIndifferentAccess#fetch String
    41.055k i/100ms
    HashWithIndifferentAccess #[] Symbol
    35.924k i/100ms
    HashWithIndifferentAccess #[] String
    44.995k i/100ms
    Hash#with_indifferent_access #fetch Symbol
    19.965k i/100ms
    Hash#with_indifferent_access #fetch String
    19.782k i/100ms
    Hash#with_indifferent_access #[] Symbol
    20.016k i/100ms
    Hash#with_indifferent_access #[] String
    23.079k i/100ms
    -------------------------------------------------
    Common Hash#fetch access
    6.053M (±10.4%) i/s - 29.864M
    Common Hash#[] access
    8.306M (±10.1%) i/s - 40.980M
    HashWithIndifferentAccess#fetch Symbol
    1.932M (±12.8%) i/s - 9.465M
    HashWithIndifferentAccess#fetch String
    1.994M (± 7.6%) i/s - 9.935M
    HashWithIndifferentAccess #[] Symbol
    1.422M (± 5.6%) i/s - 7.113M
    HashWithIndifferentAccess #[] String
    3.804M (± 9.7%) i/s - 18.808M
    Hash#with_indifferent_access #fetch Symbol
    410.034k (± 5.4%) i/s - 2.056M
    Hash#with_indifferent_access #fetch String
    414.702k (± 6.3%) i/s - 2.077M
    Hash#with_indifferent_access #[] Symbol
    381.347k (± 5.6%) i/s - 1.902M
    Hash#with_indifferent_access #[] String
    470.877k (± 6.0%) i/s - 2.354M

    Comparison:
    Common Hash#[] access: 8306261.5 i/s
    Common Hash#fetch access: 6053147.2 i/s - 1.37x slower
    HashWithIndifferentAccess #[] String: 3803546.0 i/s - 2.18x slower
    HashWithIndifferentAccess#fetch String: 1993671.6 i/s - 4.17x slower
    HashWithIndifferentAccess#fetch Symbol: 1932004.0 i/s - 4.30x slower
    HashWithIndifferentAccess #[] Symbol: 1422367.3 i/s - 5.84x slower
    Hash#with_indifferent_access #[] String: 470876.8 i/s - 17.64x slower
    Hash#with_indifferent_access #fetch String: 414701.6 i/s - 20.03x slower
    Hash#with_indifferent_access #fetch Symbol: 410033.7 i/s - 20.26x slower
    Hash#with_indifferent_access #[] Symbol: 381347.2 i/s - 21.78x slower
  4. tiagoamaro revised this gist Dec 4, 2015. 2 changed files with 84 additions and 37 deletions.
    30 changes: 29 additions & 1 deletion hash_with_indifferent_access_benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,15 @@
    require 'benchmark/ips'

    Benchmark.ips do |x|
    x.config(time: 10, warmup: 5)
    x.config(time: 5, warmup: 2)

    @hash_with_indifferent_access = ActiveSupport::HashWithIndifferentAccess.new(a: 1)
    @hash = Hash[a: 2]

    ######
    # Hash
    ######

    x.report("Common Hash#fetch access") do
    @hash.fetch :a
    end
    @@ -15,6 +19,10 @@
    @hash[:a]
    end

    ###########################
    # HashWithIndifferentAccess
    ###########################

    x.report("HashWithIndifferentAccess#fetch Symbol") do
    @hash_with_indifferent_access.fetch :a
    end
    @@ -31,6 +39,26 @@
    @hash_with_indifferent_access['a']
    end

    #########################################
    # Hash#with_indifferent_access convertion
    #########################################

    x.report("Hash#with_indifferent_access #fetch Symbol") do
    @hash.with_indifferent_access.fetch :a
    end

    x.report("Hash#with_indifferent_access #fetch String") do
    @hash.with_indifferent_access.fetch 'a'
    end

    x.report("Hash#with_indifferent_access #[] Symbol") do
    @hash.with_indifferent_access[:a]
    end

    x.report("Hash#with_indifferent_access #[] String") do
    @hash.with_indifferent_access['a']
    end

    # Compare the iterations per second of the various reports!
    x.compare!
    end
    91 changes: 55 additions & 36 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,55 @@
    # Results
    # ▶ ruby hash_with_indifferent_access_benchmark.rb
    # Calculating -------------------------------------
    # Common Hash#fetch access
    # 48.807k i/100ms
    # Common Hash#[] access
    # 49.208k i/100ms
    # HashWithIndifferentAccess#fetch Symbol
    # 41.106k i/100ms
    # HashWithIndifferentAccess#fetch String
    # 42.017k i/100ms
    # HashWithIndifferentAccess #[] Symbol
    # 38.126k i/100ms
    # HashWithIndifferentAccess #[] String
    # 47.241k i/100ms
    # -------------------------------------------------
    # Common Hash#fetch access
    # 6.135M (±10.1%) i/s - 60.521M
    # Common Hash#[] access
    # 8.485M (± 9.3%) i/s - 83.850M
    # HashWithIndifferentAccess#fetch Symbol
    # 2.136M (± 7.4%) i/s - 21.252M
    # HashWithIndifferentAccess#fetch String
    # 2.183M (± 7.0%) i/s - 21.723M
    # HashWithIndifferentAccess #[] Symbol
    # 1.537M (± 7.2%) i/s - 15.289M
    # HashWithIndifferentAccess #[] String
    # 4.029M (± 8.2%) i/s - 39.966M
    #
    # Comparison:
    # Common Hash#[] access: 8484846.1 i/s
    # Common Hash#fetch access: 6135166.1 i/s - 1.38x slower
    # HashWithIndifferentAccess #[] String: 4028542.5 i/s - 2.11x slower
    # HashWithIndifferentAccess#fetch String: 2183267.0 i/s - 3.89x slower
    # HashWithIndifferentAccess#fetch Symbol: 2136342.2 i/s - 3.97x slower
    # HashWithIndifferentAccess #[] Symbol: 1536753.4 i/s - 5.52x slower
    ▶ ruby hash_with_indifferent_access_benchmark.rb
    Calculating -------------------------------------
    Common Hash#fetch access
    49.229k i/100ms
    Common Hash#[] access
    51.376k i/100ms
    HashWithIndifferentAccess#fetch Symbol
    41.975k i/100ms
    HashWithIndifferentAccess#fetch String
    41.612k i/100ms
    HashWithIndifferentAccess #[] Symbol
    38.397k i/100ms
    HashWithIndifferentAccess #[] String
    47.435k i/100ms
    Hash#with_indifferent_access #fetch Symbol
    23.180k i/100ms
    Hash#with_indifferent_access #fetch String
    23.231k i/100ms
    Hash#with_indifferent_access #[] Symbol
    21.770k i/100ms
    Hash#with_indifferent_access #[] String
    23.082k i/100ms
    -------------------------------------------------
    Common Hash#fetch access
    5.984M (± 8.7%) i/s - 29.636M
    Common Hash#[] access
    8.188M (± 8.0%) i/s - 40.536M
    HashWithIndifferentAccess#fetch Symbol
    2.035M (± 7.1%) i/s - 10.116M
    HashWithIndifferentAccess#fetch String
    2.026M (± 7.6%) i/s - 10.070M
    HashWithIndifferentAccess #[] Symbol
    1.424M (± 6.8%) i/s - 7.103M
    HashWithIndifferentAccess #[] String
    3.740M (± 9.9%) i/s - 18.500M
    Hash#with_indifferent_access #fetch Symbol
    408.045k (± 7.6%) i/s - 2.040M
    Hash#with_indifferent_access #fetch String
    438.536k (± 6.3%) i/s - 2.184M
    Hash#with_indifferent_access #[] Symbol
    393.421k (± 7.1%) i/s - 1.959M
    Hash#with_indifferent_access #[] String
    458.767k (± 6.5%) i/s - 2.285M

    Comparison:
    Common Hash#[] access: 8187909.8 i/s
    Common Hash#fetch access: 5983764.2 i/s - 1.37x slower
    HashWithIndifferentAccess #[] String: 3740278.7 i/s - 2.19x slower
    HashWithIndifferentAccess#fetch Symbol: 2034999.4 i/s - 4.02x slower
    HashWithIndifferentAccess#fetch String: 2026388.5 i/s - 4.04x slower
    HashWithIndifferentAccess #[] Symbol: 1424228.9 i/s - 5.75x slower
    Hash#with_indifferent_access #[] String: 458767.5 i/s - 17.85x slower
    Hash#with_indifferent_access #fetch String: 438536.1 i/s - 18.67x slower
    Hash#with_indifferent_access #fetch Symbol: 408044.9 i/s - 20.07x slower
    Hash#with_indifferent_access #[] Symbol: 393421.4 i/s - 20.81x slower
  5. tiagoamaro revised this gist Dec 4, 2015. 2 changed files with 36 additions and 37 deletions.
    37 changes: 0 additions & 37 deletions hash_with_indifferent_access_benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -34,40 +34,3 @@
    # Compare the iterations per second of the various reports!
    x.compare!
    end

    # Results
    # ▶ ruby hash_with_indifferent_access_benchmark.rb
    # Calculating -------------------------------------
    # Common Hash#fetch access
    # 48.807k i/100ms
    # Common Hash#[] access
    # 49.208k i/100ms
    # HashWithIndifferentAccess#fetch Symbol
    # 41.106k i/100ms
    # HashWithIndifferentAccess#fetch String
    # 42.017k i/100ms
    # HashWithIndifferentAccess #[] Symbol
    # 38.126k i/100ms
    # HashWithIndifferentAccess #[] String
    # 47.241k i/100ms
    # -------------------------------------------------
    # Common Hash#fetch access
    # 6.135M (±10.1%) i/s - 60.521M
    # Common Hash#[] access
    # 8.485M (± 9.3%) i/s - 83.850M
    # HashWithIndifferentAccess#fetch Symbol
    # 2.136M (± 7.4%) i/s - 21.252M
    # HashWithIndifferentAccess#fetch String
    # 2.183M (± 7.0%) i/s - 21.723M
    # HashWithIndifferentAccess #[] Symbol
    # 1.537M (± 7.2%) i/s - 15.289M
    # HashWithIndifferentAccess #[] String
    # 4.029M (± 8.2%) i/s - 39.966M
    #
    # Comparison:
    # Common Hash#[] access: 8484846.1 i/s
    # Common Hash#fetch access: 6135166.1 i/s - 1.38x slower
    # HashWithIndifferentAccess #[] String: 4028542.5 i/s - 2.11x slower
    # HashWithIndifferentAccess#fetch String: 2183267.0 i/s - 3.89x slower
    # HashWithIndifferentAccess#fetch Symbol: 2136342.2 i/s - 3.97x slower
    # HashWithIndifferentAccess #[] Symbol: 1536753.4 i/s - 5.52x slower
    36 changes: 36 additions & 0 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    # Results
    # ▶ ruby hash_with_indifferent_access_benchmark.rb
    # Calculating -------------------------------------
    # Common Hash#fetch access
    # 48.807k i/100ms
    # Common Hash#[] access
    # 49.208k i/100ms
    # HashWithIndifferentAccess#fetch Symbol
    # 41.106k i/100ms
    # HashWithIndifferentAccess#fetch String
    # 42.017k i/100ms
    # HashWithIndifferentAccess #[] Symbol
    # 38.126k i/100ms
    # HashWithIndifferentAccess #[] String
    # 47.241k i/100ms
    # -------------------------------------------------
    # Common Hash#fetch access
    # 6.135M (±10.1%) i/s - 60.521M
    # Common Hash#[] access
    # 8.485M (± 9.3%) i/s - 83.850M
    # HashWithIndifferentAccess#fetch Symbol
    # 2.136M (± 7.4%) i/s - 21.252M
    # HashWithIndifferentAccess#fetch String
    # 2.183M (± 7.0%) i/s - 21.723M
    # HashWithIndifferentAccess #[] Symbol
    # 1.537M (± 7.2%) i/s - 15.289M
    # HashWithIndifferentAccess #[] String
    # 4.029M (± 8.2%) i/s - 39.966M
    #
    # Comparison:
    # Common Hash#[] access: 8484846.1 i/s
    # Common Hash#fetch access: 6135166.1 i/s - 1.38x slower
    # HashWithIndifferentAccess #[] String: 4028542.5 i/s - 2.11x slower
    # HashWithIndifferentAccess#fetch String: 2183267.0 i/s - 3.89x slower
    # HashWithIndifferentAccess#fetch Symbol: 2136342.2 i/s - 3.97x slower
    # HashWithIndifferentAccess #[] Symbol: 1536753.4 i/s - 5.52x slower
  6. tiagoamaro revised this gist Dec 4, 2015. 1 changed file with 29 additions and 18 deletions.
    47 changes: 29 additions & 18 deletions hash_with_indifferent_access_benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -2,15 +2,19 @@
    require 'benchmark/ips'

    Benchmark.ips do |x|
    x.config(time: 5, warmup: 2)
    x.config(time: 10, warmup: 5)

    @hash_with_indifferent_access = ActiveSupport::HashWithIndifferentAccess.new(a: 1)
    @hash = Hash[a: 2]
    @hash = Hash[a: 2]

    x.report("Common Hash access") do
    x.report("Common Hash#fetch access") do
    @hash.fetch :a
    end

    x.report("Common Hash#[] access") do
    @hash[:a]
    end

    x.report("HashWithIndifferentAccess#fetch Symbol") do
    @hash_with_indifferent_access.fetch :a
    end
    @@ -34,29 +38,36 @@
    # Results
    # ▶ ruby hash_with_indifferent_access_benchmark.rb
    # Calculating -------------------------------------
    # Common Hash access 48.481k i/100ms
    # Common Hash#fetch access
    # 48.807k i/100ms
    # Common Hash#[] access
    # 49.208k i/100ms
    # HashWithIndifferentAccess#fetch Symbol
    # 39.126k i/100ms
    # 41.106k i/100ms
    # HashWithIndifferentAccess#fetch String
    # 37.910k i/100ms
    # 42.017k i/100ms
    # HashWithIndifferentAccess #[] Symbol
    # 33.985k i/100ms
    # 38.126k i/100ms
    # HashWithIndifferentAccess #[] String
    # 42.670k i/100ms
    # 47.241k i/100ms
    # -------------------------------------------------
    # Common Hash access 5.650M (±10.1%) i/s - 27.877M
    # Common Hash#fetch access
    # 6.135M (±10.1%) i/s - 60.521M
    # Common Hash#[] access
    # 8.485M (± 9.3%) i/s - 83.850M
    # HashWithIndifferentAccess#fetch Symbol
    # 1.953M8.5%) i/s - 9.703M
    # 2.136M7.4%) i/s - 21.252M
    # HashWithIndifferentAccess#fetch String
    # 1.985M (± 7.8%) i/s - 9.857M
    # 2.183M (± 7.0%) i/s - 21.723M
    # HashWithIndifferentAccess #[] Symbol
    # 1.410M (± 7.2%) i/s - 7.035M
    # 1.537M (± 7.2%) i/s - 15.289M
    # HashWithIndifferentAccess #[] String
    # 3.704M10.0%) i/s - 18.305M
    # 4.029M 8.2%) i/s - 39.966M
    #
    # Comparison:
    # Common Hash access: 5649613.0 i/s
    # HashWithIndifferentAccess #[] String: 3703535.0 i/s - 1.53x slower
    # HashWithIndifferentAccess#fetch String: 1984536.9 i/s - 2.85x slower
    # HashWithIndifferentAccess#fetch Symbol: 1953020.8 i/s - 2.89x slower
    # HashWithIndifferentAccess #[] Symbol: 1410364.2 i/s - 4.01x slower
    # Common Hash#[] access: 8484846.1 i/s
    # Common Hash#fetch access: 6135166.1 i/s - 1.38x slower
    # HashWithIndifferentAccess #[] String: 4028542.5 i/s - 2.11x slower
    # HashWithIndifferentAccess#fetch String: 2183267.0 i/s - 3.89x slower
    # HashWithIndifferentAccess#fetch Symbol: 2136342.2 i/s - 3.97x slower
    # HashWithIndifferentAccess #[] Symbol: 1536753.4 i/s - 5.52x slower
  7. tiagoamaro created this gist Dec 4, 2015.
    62 changes: 62 additions & 0 deletions hash_with_indifferent_access_benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    require 'active_support/all'
    require 'benchmark/ips'

    Benchmark.ips do |x|
    x.config(time: 5, warmup: 2)

    @hash_with_indifferent_access = ActiveSupport::HashWithIndifferentAccess.new(a: 1)
    @hash = Hash[a: 2]

    x.report("Common Hash access") do
    @hash.fetch :a
    end

    x.report("HashWithIndifferentAccess#fetch Symbol") do
    @hash_with_indifferent_access.fetch :a
    end

    x.report("HashWithIndifferentAccess#fetch String") do
    @hash_with_indifferent_access.fetch 'a'
    end

    x.report("HashWithIndifferentAccess #[] Symbol") do
    @hash_with_indifferent_access[:a]
    end

    x.report("HashWithIndifferentAccess #[] String") do
    @hash_with_indifferent_access['a']
    end

    # Compare the iterations per second of the various reports!
    x.compare!
    end

    # Results
    # ▶ ruby hash_with_indifferent_access_benchmark.rb
    # Calculating -------------------------------------
    # Common Hash access 48.481k i/100ms
    # HashWithIndifferentAccess#fetch Symbol
    # 39.126k i/100ms
    # HashWithIndifferentAccess#fetch String
    # 37.910k i/100ms
    # HashWithIndifferentAccess #[] Symbol
    # 33.985k i/100ms
    # HashWithIndifferentAccess #[] String
    # 42.670k i/100ms
    # -------------------------------------------------
    # Common Hash access 5.650M (±10.1%) i/s - 27.877M
    # HashWithIndifferentAccess#fetch Symbol
    # 1.953M (± 8.5%) i/s - 9.703M
    # HashWithIndifferentAccess#fetch String
    # 1.985M (± 7.8%) i/s - 9.857M
    # HashWithIndifferentAccess #[] Symbol
    # 1.410M (± 7.2%) i/s - 7.035M
    # HashWithIndifferentAccess #[] String
    # 3.704M (±10.0%) i/s - 18.305M
    #
    # Comparison:
    # Common Hash access: 5649613.0 i/s
    # HashWithIndifferentAccess #[] String: 3703535.0 i/s - 1.53x slower
    # HashWithIndifferentAccess#fetch String: 1984536.9 i/s - 2.85x slower
    # HashWithIndifferentAccess#fetch Symbol: 1953020.8 i/s - 2.89x slower
    # HashWithIndifferentAccess #[] Symbol: 1410364.2 i/s - 4.01x slower