From: Joel VanderWerf Date: 2008-09-11T04:45:02+09:00 Subject: Re: How to operate on 2 arrays simultaneously? ara.t.howard wrote: > > On Sep 10, 2008, at 1:03 PM, matt neuburg wrote: > >> Everything in Ruby is a pointer. If a[0] is a pointer to a "big long >> string", then a.zip(b)[0][0] is a pointer to the very same "big long >> string" - not a copy of the string. So creating the "huge_new_array" >> just creates some new pointers, right? And pointers are very small. So >> for huge_new_array to be a problem, the existence of a and b would have >> to have been problematic to start with - meaning that they would have to >> have huge length. The amount of *data* in the story (stuff like "big >> long string") is not increased. > > not really > > > cfp:~ > cat a.rb > mb = 2 ** 20 > > big = 2 * mb > > a = Array.new big > b = a.dup > > memory_usage = `ps -o rss= -p #{Process.pid}`.to_i > puts memory_usage > > a.zip(b) > > memory_usage = `ps -o rss= -p #{Process.pid}`.to_i > puts memory_usage It's less dramatic when the entries are not nil (or fixnum etc)--I think that's Matt's point. mb = 2 ** 20 big = 2 * mb a = Array.new big do |i| "the string for entry #{i}" end b = a.dup memory_usage = `ps -o rss= -p #{Process.pid}`.to_i puts memory_usage a.zip(b) memory_usage = `ps -o rss= -p #{Process.pid}`.to_i puts memory_usage __END__ 213256 322232 -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407