From: Robert Klemme Date: 2009-11-10T06:35:26+09:00 Subject: Re: Good or best way to allocate a large array On 11/09/2009 06:25 PM, Ralph Shnelvar wrote: > Newbie here: > > I will want/need to create an array of approximately 300K rows and 20 > columns. The array will be an array of either 32-bit quantities or 64-bit quantities. So we are talking about 300,000 * 20 * 8 ~ 48MB which does not sound too large. robert@fussel:~$ time ruby1.9 -e 'x=1<<63;Array.new(300_000*20) {x}' real 0m1.922s user 0m1.288s sys 0m0.032s robert@fussel:~$ time ruby1.9 -e 'x=1<<63;Array.new(300_000*20) {1<<63}' real 0m7.143s user 0m6.668s sys 0m0.204s > Does Ruby support native 64-bit quantities? Yes. You can try it out ruby1.9 -e '100.times {|i| x = 1 << i;p x, x.class}' > What's a good/best way to to create such an array. Is there a way to > tell Ruby, "OK, I'm done with this array." No need to: if there are no more live references to that Array it will get collected eventually. > Can one create such an array such that the garbage collector can > free the memory in one fast operation? Probably not because of the number of objects in the Array (if they are all Fixnums it will probably a bit faster. The question is, what do you do with that huge Array? Chances are that you need to allocate more memory during processing. What's your use case? A database might be an option or a flat file - it all depends. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/