From: Brad Phelan Date: 2007-05-31T18:10:29+09:00 Subject: Re: Enumerable#serially - those nifty functions w/o memory footprint > > You are changing subject in between: Originally you started out with > memory consumption being the issue. Now you talk about speed. Your > solution is neither fast nor easy on the memory. For speed see the > benchmark below. It's not easy on the memory because you still create a > *ton* of temporary one element arrays (four per iteration if I'm not > mistaken) - this avoids the single large copy but allocating and GC'ing > all these temporaries imposes a significant overhead. The obvious and > simple solution here is to do it all in *one* iteration. So why invent > something new if there is an easy and efficient solution available? I can get comparable results for my solution Rehearsal -------------------------------------------------- serially 7.410000 0.120000 7.530000 ( 7.731576) classic 5.890000 0.080000 5.970000 ( 6.123102) ---------------------------------------- total: 13.500000sec user system total real serially 6.500000 0.000000 6.500000 ( 6.581172) classic 3.160000 0.010000 3.170000 ( 3.199095) See http://xtargets.com/snippets/posts/show/69 for details What kills me and makes the final overhead is a fast way to *non-recursively* flatten an array of arrays. My solution is def self.flatten(o) length = o.inject(0) {|m,i|m+i.length} out = Array.new(length) base = 0 o.each do |sub| top = base + sub.length out[base..top]=sub base = top end end It is much faster than the inject and concat simple method but still too slow. Any ideas ( apart from code it in C ) Brad