Skip to content

Instantly share code, notes, and snippets.

@headius
Last active March 26, 2021 04:22
Show Gist options
  • Save headius/28343b8c393e76c717314af57089848d to your computer and use it in GitHub Desktop.
Save headius/28343b8c393e76c717314af57089848d to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
puts RUBY_DESCRIPTION
array = [[[[[[[[[[[14]]]]]]]]]]]
Benchmark.ips do |x|
x.report('dig-01') {|i| while i>0; i-=1; array.dig(0); end }
x.report('dig-02') {|i| while i>0; i-=1; array.dig(0, 0); end }
x.report('dig-03') {|i| while i>0; i-=1; array.dig(0, 0, 0); end }
x.report('dig-04') {|i| while i>0; i-=1; array.dig(0, 0, 0, 0); end }
x.report('dig-05') {|i| while i>0; i-=1; array.dig(0, 0, 0, 0, 0); end } # easy to find examples this long
x.report('dig-06') {|i| while i>0; i-=1; array.dig(0, 0, 0, 0, 0, 0); end }
x.report('dig-07') {|i| while i>0; i-=1; array.dig(0, 0, 0, 0, 0, 0, 0); end }
x.report('dig-08') {|i| while i>0; i-=1; array.dig(0, 0, 0, 0, 0, 0, 0, 0); end } # possible to find examples this long
x.report('dig-09') {|i| while i>0; i-=1; array.dig(0, 0, 0, 0, 0, 0, 0, 0, 0); end }
x.report('dig-10') {|i| while i>0; i-=1; array.dig(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); end }
x.compare!
end
These numbers are in iterations per second.
Before (asCollector):
dig-01 21.941M (± 8.0%) i/s - 109.100M in 5.009685s
dig-02 19.852M (±11.7%) i/s - 98.315M in 5.051134s
dig-03 14.844M (±10.7%) i/s - 74.098M in 5.054806s
dig-04 13.658M (± 7.5%) i/s - 68.358M in 5.033005s
dig-05 12.225M (± 8.0%) i/s - 61.502M in 5.065112s
dig-06 11.466M (± 5.7%) i/s - 57.320M in 5.016439s
dig-07 10.479M (± 4.6%) i/s - 52.376M in 5.008984s
dig-08 8.805M (± 7.2%) i/s - 44.258M in 5.052483s
dig-09 8.658M (± 8.7%) i/s - 43.382M in 5.053520s
dig-10 8.336M (± 9.1%) i/s - 41.584M in 5.040264s
After (collectArguments):
dig-01 86.627M (±13.0%) i/s - 425.185M in 5.015319s
dig-02 69.060M (± 6.9%) i/s - 347.973M in 5.064471s
dig-03 32.177M (±11.4%) i/s - 159.695M in 5.046041s
dig-04 27.283M (± 7.7%) i/s - 137.671M in 5.078526s
dig-05 21.859M (± 5.6%) i/s - 110.144M in 5.054894s
dig-06 19.737M (± 8.2%) i/s - 98.814M in 5.043686s
dig-07 15.043M (±11.7%) i/s - 74.727M in 5.050454s
dig-08 16.181M (± 7.6%) i/s - 80.414M in 5.004321s
dig-09 13.739M (± 5.8%) i/s - 69.735M in 5.093540s
dig-10 13.340M (± 5.8%) i/s - 67.908M in 5.109316s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment