4

I have a large array (10+ million objects) that I want to combine into a single object (for simplicity, let's assume here a string) and run each element in the array through some processing (abstracted by the function do_stuff). There are two natural ways to do this:

memo = ""
big_array.each do |e|
  memo << do_stuff(e)
end
memo

and

big_array.reduce("") do |memo, e|
  memo + do_stuff(e)
end

The reduce/inject syntax is more aesthetically appealing, but the question is which is more memory efficient. We already know that each is marginally more time efficient than reduce, but what about memory?

I'm also not clear on how I could profile memory usage in Ruby, especially over just a block of code, so if someone can provide some pointers to that I would also appreciate it.

Community
  • 1
  • 1
Gordon Seidoh Worley
  • 7,839
  • 6
  • 45
  • 82
  • I think it depends a lot on what `memo` is. Here, in the first case, string is mutated and you don't create a new instance in every iteration, as you do in the second example. – Mladen Jablanović Apr 11 '13 at 13:01
  • In your second example you could also use `<<` instead of `+` (although `each_with_object` would be better choice then). In current shape I'm pretty sure that latter is much worse in terms of memory consumption (especially if `memo` tends to grow large). – samuil Apr 11 '13 at 13:05

1 Answers1

-1

I'm pretty sure that with map reduce (as written in your example) you are creating a new string each time +).

Using << you are modifying the original string. This should be better as less GC cycles will be triggered.

You mentioned that you are using a string as an example; so it's difficult to answer for your real problem.

Bert Goethals
  • 7,867
  • 2
  • 20
  • 32