巷の噂で Ruby の Array#<< が Array#push よりも速いと聞いたので調べてみた。まずはベンチマークを取ってみた。 require 'benchmark' Benchmark.bm 10 do |r| r.report "push" do 8000000.times do; [].push(1); end end r.report "<<" do 8000000.times do; [] <<1; end end end 結果は以下の通り。 user system total real push 1.570000 0.000000 1.570000 ( 1.579687) << 1.280000 0.000000 1.280000 ( 1.288951) 確かに Array#<< の
![Big Sky :: Ruby の Array#<< は Array#push よりも速いか](https://cdn-ak-scissors.b.st-hatena.com/image/square/ae5f360d3c82cd87e23b34fb6f5013ab7cf0f1a6/height=288;version=1;width=512/https%3A%2F%2Fmattn.kaoriya.net%2Fimages%2Flogo.png)