From: Robert Klemme Date: 2008-06-27T20:41:57+09:00 Subject: Re: Performance improvement possible? 2008/6/27 Eleanor McHugh : > I'm surprised at the behaviour you're seeing so I ran a quick benchmark on > what I believe to be an equivalent array handling problem with my laptop (OS > X, Ruby 1.8.6-p111, Core 2 Duo 2GHz, 2GB RAM): > > require 'benchmark' > include Benchmark > bm(6) do |y| > y.report("appending") { x = []; (1..20000000).each { |i| x << i } } > y.report("nested creation") { x = Array.new(1000) { Array.new(1000) { > Array.new(20, 0) } } } > y.report("unrolling") { x.flatten.flatten.length } > end This can't be the exact code you were benchmarking since "x" is undefined in the "unrolling" test. Also, why do you flatten twice? You can't beat an Array flatter than flatten. :-) irb(main):009:0> Array.new(1000) { Array.new(1000) { Array.new(20, 0) } }.flatten.select {|x| Array === x}.empty? => true > Regardless of what the profiler says, any program that's opening 32000 files > and writing to them is a prime candidate for IO optimisation... ... because IO in that case is also likely the slowest bit. Absolutely. Kind regards robert -- use.inject do |as, often| as.you_can - without end