Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2012 03:06
Show Gist options
  • Save anonymous/4342628 to your computer and use it in GitHub Desktop.
Save anonymous/4342628 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'benchmark/ips'
other = "test"
n = 100_000
Benchmark.ips do |x|
x.report('#{...}') { n.times { "test #{other} test" } }
x.report('#<<') { n.times { "test " << other << " test" } }
x.report('#+') { n.times { "test " + other + " test" } }
end
# $ ruby string_test.rb
# Calculating -------------------------------------
# #{...} 1 i/100ms
# #<< 1 i/100ms
# #+ 1 i/100ms
# -------------------------------------------------
# #{...} 6.4 (±31.2%) i/s - 30 in 5.039693s
# #<< 7.8 (±12.8%) i/s - 38 in 5.026323s
# #+ 5.9 (±16.9%) i/s - 29 in 5.050913s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment