Created
May 28, 2019 13:39
-
-
Save rmm5t/bfd42a3089a7c7d2df8ca410784c1e60 to your computer and use it in GitHub Desktop.
Benchmark String interpolation vs Array#join
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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