From: Christian Boos Date: 2002-08-15T16:08:11+09:00 Subject: RE: Is this array operation correct? > -----Original Message----- > From: George Ogata [mailto:g_ogata@optushome.com.au] > Sent: Thursday, August 15, 2002 4:14 AM > To: ruby-talk ML > Subject: RE: Is this array operation correct? > > > > Then, do something like: > > > > Array.new(3).map { [] } > > > > Is there something shorter/more efficient? > > If you don't like using Array.new, you can #map to a Range: > > (1..3).map{[]} > > (Dunno about efficiency, here.) > > If you're going to map to a fresh Array, though, you may as well use the > in-place version: > > Array.new(3).map!{[]} > > # Really basic benchmarking (for 1.6.5 ) ... def time_it GC.start now = Time.now yield Time.now - now end time_it { Array.new(100000).map {[]} } 0.611 time_it { Array.new(100000).map {[]} } 0.631 time_it { (1..100000).map {[]} } 0.471 time_it { (1..100000).map {[]} } 0.471 time_it { Array.new(100000).map! {[]} } 0.351 time_it { Array.new(100000).map! {[]} } 0.361 Using map! seems the fastest way, thanks for the tip! -- Christian