From: Eleanor McHugh Date: 2008-06-27T20:59:12+09:00 Subject: Re: Performance improvement possible? On 27 Jun 2008, at 12:41, Robert Klemme wrote: > 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. I was using IRB and x was already defined earlier in my session, so yes and no: for my session it was defined, but for this snippet it won't be. > Also, why do you flatten twice? > You can't beat an Array flatter than flatten. :-) I was posting at 2am >;o Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason