Question

I have an array of Elements, and each element has a property :image.

I would like an array of :images, so whats the quickest and least expensive way to achieve this. Is it just iteration over the array and push each element into a new array, something like this:

images = []
elements.each {|element| images << element.image}
Was it helpful?

Solution

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.

OTHER TIPS

elements.map {|element| element.image}

This should have about the same performance as your version, but is somewhat more succinct and more idiomatic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top