Skip to content

Instantly share code, notes, and snippets.

@fadhlirahim
Last active June 1, 2016 07:57
Show Gist options
  • Save fadhlirahim/b7d39fadfe19b6d121285837dab41250 to your computer and use it in GitHub Desktop.
Save fadhlirahim/b7d39fadfe19b6d121285837dab41250 to your computer and use it in GitHub Desktop.
Some benchmarking using benchmark-ips on some common ruby methods
# gem install benchmark-ips
# run irb
require "benchmark/ips"
n = 100_000
# Array#push vs Array#<<
#
# Results:
#
# Warming up --------------------------------------
# push 10.000 i/100ms
# << 13.000 i/100ms
# Calculating -------------------------------------
# push 103.141 (± 3.9%) i/s - 520.000 in 5.050541s
# << 133.237 (± 3.0%) i/s - 676.000 in 5.077262s
# Comparison:
# <<: 133.2 i/s
# push: 103.1 i/s - 1.29x slower
#
# Code
countries = []
VARIOUS_COUNTRY = "zz".freeze
Benchmark.ips do |r|
r.report('push') { n.times {countries.push(VARIOUS_COUNTRY)} }
r.report('<<') { n.times {countries << VARIOUS_COUNTRY } }
r.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment