From: Maciej Tomaka Date: 2008-07-30T18:24:41+09:00 Subject: Re: Interesting Array Initialization Typo The correct benchmark is: (look how long x.times takes :) require 'benchmark' include Benchmark bmbm do |t| t.report("<<") do 100_000.times do a = [] a << [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a << [ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] a << [ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29] a << [ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39] a << [ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49] a << [ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59] a << [ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69] a << [ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79] a << [ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89] a << [ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] end end t.report("[]") do 100_000.times do a = [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] ] end end t.report("NO") do 100_000.times do 100.times do |x| end end end end Rehearsal -------------------------------------- << 1.020000 0.020000 1.040000 ( 1.047729) [] 0.730000 0.010000 0.740000 ( 0.741970) NO 4.270000 1.510000 5.780000 ( 5.808074) ----------------------------- total: 7.560000sec user system total real << 1.000000 0.020000 1.020000 ( 1.032165) [] 0.740000 0.010000 0.750000 ( 0.743470) NO 5.070000 1.560000 6.630000 ( 6.634026) So << and [] are comparable. Shadowfirebird has good solution inmho w/o significant perfomance impact. -- Posted via http://www.ruby-forum.com/.