Skip to content

Instantly share code, notes, and snippets.

@rmm5t
Created May 28, 2019 13:39
Show Gist options
  • Select an option

  • Save rmm5t/bfd42a3089a7c7d2df8ca410784c1e60 to your computer and use it in GitHub Desktop.

Select an option

Save rmm5t/bfd42a3089a7c7d2df8ca410784c1e60 to your computer and use it in GitHub Desktop.

Revisions

  1. rmm5t created this gist May 28, 2019.
    19 changes: 19 additions & 0 deletions benchmark_interpolation_vs_join.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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)