Help us understand the problem. What is going on with this article?

Ruby の Array でまとめて push / unshift

More than 1 year has passed since last update.

Ruby の配列操作である Array#pushArray#unshift は複数の引数を取ることが出来ます。

まとめて push

list = [1, 2, 3, 4, 5]

a = []
b = []

list.each do |n|
  a.push(n)
end

b.push(*list)

p a #=> [1, 2, 3, 4, 5]
p b #=> [1, 2, 3, 4, 5]

まとめて unshift

list = [1, 2, 3, 4, 5]

a = []
b = []

list.each do |n|
  a.unshift(n)
end

b.unshift(*list.reverse)

p a #=> [5, 4, 3, 2, 1]
p b #=> [5, 4, 3, 2, 1]

まとめて操作したほうが処理速度も速くなります。

push の比較

require 'benchmark/ips'

MAX_N = 10000
list = Array.new(MAX_N) { rand(MAX_N) }

Benchmark.ips do |x|
  x.report("#each_push") do
    a = []
    list.each do |n|
      a.push(n)
    end
  end

  x.report("#bulk_push") do
    a = []
    a.push(*list)
  end

  x.compare!
end

結果

Warming up --------------------------------------
          #each_push   146.000  i/100ms
          #bulk_push     3.714k i/100ms
Calculating -------------------------------------
          #each_push      1.506k (± 5.2%) i/s -      7.592k in   5.053872s
          #bulk_push     46.680k (±29.7%) i/s -    219.126k in   5.013369s

Comparison:
          #bulk_push:    46679.9 i/s
          #each_push:     1506.5 i/s - 30.99x  slower

unshift の比較

require 'benchmark/ips'

MAX_N = 10000
list = Array.new(MAX_N) { rand(MAX_N) }

Benchmark.ips do |x|
  x.report("#each_unshift") do
    a = []
    list.each do |n|
      a.unshift(n)
    end
  end

  x.report("#bulk_unshift") do
    a = []
    a.unshift(*list.reverse)
  end

  x.compare!
end

結果

Warming up --------------------------------------
       #each_unshift   147.000  i/100ms
       #bulk_unshift     1.971k i/100ms
Calculating -------------------------------------
       #each_unshift      1.542k (± 5.4%) i/s -      7.791k in   5.067292s
       #bulk_unshift     23.891k (±27.8%) i/s -    114.318k in   5.113775s

Comparison:
       #bulk_unshift:    23890.6 i/s
       #each_unshift:     1542.4 i/s - 15.49x  slower

参考サイト

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away