Skip to content

Instantly share code, notes, and snippets.

@marshall-lee
Created February 2, 2016 00:08
Show Gist options
  • Save marshall-lee/6ae5dffb12b9abcb1940 to your computer and use it in GitHub Desktop.
Save marshall-lee/6ae5dffb12b9abcb1940 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
puts "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}:\n"
require 'benchmark/ips'
puts
puts '* #to_s:'
puts
Benchmark.ips do |x|
value = 'Totoro'
x.report('%s formatter') do
'Hello, %s!' % value
end
x.report('interpolation') do
"Hello, #{value}!"
end
x.compare!
end
puts
puts '* #inspect:'
puts
Benchmark.ips do |x|
value = { x: 123, y: 456 }
x.report('%p formatter') do
'Hello, %p!' % value
end
x.report('interpolation') do
"Hello, #{value.inspect}!"
end
x.compare!
end
# Ruby 2.3.0-p0:
#
# * #to_s:
#
# Calculating -------------------------------------
# %s formatter 47.512k i/100ms
# interpolation 83.395k i/100ms
# -------------------------------------------------
# %s formatter 1.074M (± 5.3%) i/s - 5.369M
# interpolation 2.688M (± 3.2%) i/s - 13.427M
#
# Comparison:
# interpolation: 2687669.1 i/s
# %s formatter: 1073790.1 i/s - 2.50x slower
#
#
# * #inspect:
#
# Calculating -------------------------------------
# %p formatter 15.685k i/100ms
# interpolation 20.331k i/100ms
# -------------------------------------------------
# %p formatter 218.396k (± 1.4%) i/s - 1.098M
# interpolation 298.252k (± 1.4%) i/s - 1.504M
#
# Comparison:
# interpolation: 298252.1 i/s
# %p formatter: 218396.3 i/s - 1.37x slower
#
#
#
# Ruby 2.2.4-p230:
#
# * #to_s:
#
# Calculating -------------------------------------
# %s formatter 40.533k i/100ms
# interpolation 75.439k i/100ms
# -------------------------------------------------
# %s formatter 1.027M (± 2.4%) i/s - 5.148M
# interpolation 2.489M (± 0.5%) i/s - 12.447M
#
# Comparison:
# interpolation: 2488572.8 i/s
# %s formatter: 1026828.9 i/s - 2.42x slower
#
#
# * #inspect:
#
# Calculating -------------------------------------
# %p formatter 15.235k i/100ms
# interpolation 20.327k i/100ms
# -------------------------------------------------
# %p formatter 207.339k (± 1.4%) i/s - 1.051M
# interpolation 295.857k (± 1.6%) i/s - 1.484M
#
# Comparison:
# interpolation: 295856.9 i/s
# %p formatter: 207338.8 i/s - 1.43x slower
#
#
#
# Ruby 2.1.7-p400:
#
# * #to_s:
#
# Calculating -------------------------------------
# %s formatter 46.336k i/100ms
# interpolation 81.078k i/100ms
# -------------------------------------------------
# %s formatter 1.057M (± 2.2%) i/s - 5.282M
# interpolation 2.387M (± 1.7%) i/s - 12.000M
#
# Comparison:
# interpolation: 2386831.6 i/s
# %s formatter: 1056918.5 i/s - 2.26x slower
#
#
# * #inspect:
#
# Calculating -------------------------------------
# %p formatter 15.573k i/100ms
# interpolation 20.277k i/100ms
# -------------------------------------------------
# %p formatter 203.965k (± 1.7%) i/s - 1.028M
# interpolation 303.404k (± 0.6%) i/s - 1.521M
#
# Comparison:
# interpolation: 303404.4 i/s
# %p formatter: 203964.8 i/s - 1.49x slower
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment