Array.concat(a) vs array.push(*a)

It feels to me like array.concat seems slower than the equivalent (I think) variation using array.push:

a = [1,2,3,4,5]; b=; puts Benchmark.measure{ (1..100000).each{ b.push(*a)}}

   0.090000 0.000000 0.090000 ( 0.090436)

a = [1,2,3,4,5]; b=; puts Benchmark.measure{ (1..100000).each{ b.concat(a)}}

   0.290000 0.000000 0.290000 ( 0.296501)

(using ruby 1.8.4)

This surprised me somewhat, because earlier
(http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/7521\)
it was suggested that concat is a fast way of appending to arrays.

Is there something I'm missing, or does it seem I should be
using push(*a) in places where I had been using concat(a)?

Hmm... perhaps answering my own question ....
array.push(*a) segfaults more than concat does....

> a = [1,2,3,4,5]; puts Benchmark.measure{ (1..20).each{ a.concat(a)}}

   0.100000 0.040000 0.140000 ( 0.143773)
=> nil

> a = [1,2,3,4,5]; puts Benchmark.measure{ (1..20).each{ a.push(*a)}}

Segmentation fault

···

On Mon, 24 Apr 2006, rm_rails@cheapcomplexdevices.com wrote:

It feels to me like array.concat seems slower than the equivalent (I think) variation using array.push:

a = [1,2,3,4,5]; b=; puts Benchmark.measure{ (1..100000).each{ b.push(*a)}}

0.090000 0.000000 0.090000 ( 0.090436)

a = [1,2,3,4,5]; b=; puts Benchmark.measure{ (1..100000).each{ b.concat(a)}}

0.290000 0.000000 0.290000 ( 0.296501)

(using ruby 1.8.4)

This surprised me somewhat, because earlier
(http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/7521\)
it was suggested that concat is a fast way of appending to arrays.

Is there something I'm missing, or does it seem I should be
using push(*a) in places where I had been using concat(a)?

Very interesting, as a matter concat is faster - as shown below- under
*normal* circumstances
obviously push is faster for very very long receivers.
Can somebody explain why, I could study the code, but I am not sure I would
find it even if I had the time.
Cheers
cat array.rb; ruby array.rb
require 'benchmark'
a = [1,2,3,4,5]; puts Benchmark.measure{ (1..1000000).each{
b=;b.push(*a)}}
a = [1,2,3,4,5]; puts Benchmark.measure{ (1..1000000).each{ b=; b.concat
(a)}}
  2.320000 0.000000 2.320000 ( 2.938714)
  1.980000 0.000000 1.980000 ( 2.301109)

Robert

···

On 4/24/06, rm_rails@cheapcomplexdevices.com < rm_rails@cheapcomplexdevices.com> wrote:

It feels to me like array.concat seems slower than the equivalent (I
think) variation using array.push:

>> a = [1,2,3,4,5]; b=; puts Benchmark.measure{ (1..100000).each{ b.push
(*a)}}
   0.090000 0.000000 0.090000 ( 0.090436)

>> a = [1,2,3,4,5]; b=; puts Benchmark.measure{ (1..100000).each{
b.concat(a)}}
   0.290000 0.000000 0.290000 ( 0.296501)

(using ruby 1.8.4)

This surprised me somewhat, because earlier
(http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/7521\)
it was suggested that concat is a fast way of appending to arrays.

Is there something I'm missing, or does it seem I should be
using push(*a) in places where I had been using concat(a)?

--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein