Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Last active August 29, 2015 14:19
Show Gist options
  • Save TrevorS/7007588e44ab53cc4491 to your computer and use it in GitHub Desktop.
Save TrevorS/7007588e44ab53cc4491 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
require 'benchmark'
iterations = 5_000_000
a = 'test a'
b = 'test b'
c = 'test c'
Benchmark.bmbm(15) do |bm|
bm.report('a + b + c') do
0.upto(iterations) do |i|
a + " " + b + " " + c
end
end
bm.report('interpolation') do
0.upto(iterations) do |i|
"#{a} #{b} #{c}"
end
end
bm.report('[].join') do
0.upto(iterations) do |i|
[a, b, c].join(' ')
end
end
end
# Rehearsal ---------------------------------------------------
# a + b + c 2.060000 0.000000 2.060000 ( 2.060781)
# interpolation 1.600000 0.000000 1.600000 ( 1.604380)
# [].join 3.440000 0.000000 3.440000 ( 3.443946)
# ------------------------------------------ total: 7.100000sec
# user system total real
# a + b + c 2.070000 0.000000 2.070000 ( 2.073246)
# interpolation 1.600000 0.000000 1.600000 ( 1.599978)
# [].join 3.420000 0.010000 3.430000 ( 3.422545)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment