You can use the Benchmark module to test these sorts of things. I ran @sepp2k's version against your original code like so:
require 'benchmark'
class Element
attr_accessor :image
def initialize(image)
@image = image
end
end
elements = Array.new(500) {|index| Element.new(index)}
n = 10000
Benchmark.bm do |x|
x.report do
n.times do
# Globalkeith's version
image = []
elements.each {|element| image << element.image}
end
end
# sepp2k's version
x.report { n.times do elements.map {|element| element.image} end }
end
The output on my machine was consistently (after more than 3 runs) very close to this:
user system total real
2.140000 0.000000 2.140000 ( 2.143290)
1.420000 0.010000 1.430000 ( 1.422651)
Thus demonstrating that map
is significantly faster than manually appending to an array when the array is somewhat large and the operation is performed many times.