Skip to content

Append in place for strings and arrays [failure unrelated]

gitlab-qa-bot requested to merge github/fork/cirosantilli/append-inplace into master

Created by: cirosantilli

It's faster.

Behavior should be unchanged: no function inputs were altered, only local variables.

Benchmark:

Benchmark.bm(first_col_width) do |benchmark|
  benchmark.report('+= [0]') do
    a = []
    N.times { a += [0] }
  end

  benchmark.report('<< 0') do
    a = []
    N.times { a << 0 }
  end

  benchmark.report('<< [0, 1]') do
    a = []
    N.times { a += [0, 1] }
  end

  benchmark.report('push(*[0, 1])') do
    a = []
    N.times { a.push(*[0, 1]) }
  end

  benchmark.report('+= "ab"') do
    a = ''
    N.times { a += 'ab' }
  end

  benchmark.report('<< "ab"') do
    a = []
    N.times { a << 'ab' }
  end
end

Result:

                    user     system      total        real
+= [0]             0.090000   0.010000   0.100000 (  0.093640)
<< 0               0.000000   0.000000   0.000000 (  0.000706)
<< [0, 1]          0.170000   0.000000   0.170000 (  0.164410)
push(*[0, 1])      0.000000   0.000000   0.000000 (  0.001339)
+= "ab"            0.020000   0.000000   0.020000 (  0.024198)
<< "ab"            0.000000   0.000000   0.000000 (  0.001242)

Merge request reports