Skip to content

Instantly share code, notes, and snippets.

@rmm5t
Created May 28, 2019 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmm5t/bfd42a3089a7c7d2df8ca410784c1e60 to your computer and use it in GitHub Desktop.
Save rmm5t/bfd42a3089a7c7d2df8ca410784c1e60 to your computer and use it in GitHub Desktop.
Benchmark String interpolation vs Array#join
require 'benchmark'
def test_interpolation(x, y)
"X#{x}Y#{y}"
end
def test_join(x, y)
["X", x, "Y", y].join
end
n = 100_000
Benchmark.bmbm(15) do |x|
x.report("String interpolation") { n.times { test_interpolation(1, 2) } }
x.report("Array#join") { n.times { test_join(1, 2) } }
end
# >> user system total real
# >> String interpolation 0.030083 0.000099 0.030182 ( 0.030252)
# >> Array#join 0.080881 0.000200 0.081081 ( 0.081173)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment