a << i is faster than a.push(e). I suspect it's because #push takes *args, and the overhead from that is relatively slower than <<, which takes a single arg.
benchmark.rb
```ruby
require "benchmark/ips"
a = []
Benchmark.ips do |x|
x.report("push") do |times|
i = 0
a.clear
while (i += 1) < times
a.push(i)
end
end
x.report("<<") do |times|
i = 0
a.clear
while (i += 1) < times
a << i
end
end
x.compare!
end
```
a << i
is faster thana.push(e)
. I suspect it's because#push
takes*args
, and the overhead from that is relatively slower than<<
, which takes a single arg.
```ruby require "benchmark/ips" a = [] Benchmark.ips do |x| x.report("push") do |times| i = 0 a.clear while (i += 1) < times a.push(i) end end x.report("<<") do |times| i = 0 a.clear while (i += 1) < times a << i end end x.compare! end ```benchmark.rb