From: Sander Land Date: 2006-09-04T01:43:40+09:00 Subject: Re: Happy Numbers (#93) On 9/3/06, Phrogz wrote: > Sander Land wrote: > > class Array > > def sum > > inject(0){|a,b| a+b} > > end > > end > > For a little more speed, try instead: > > class Array > def sum > inject{ |a,b| a+b } > end > end Is that really so much faster? Worth breaking on empty arrays? Just had to test if there was any performance difference, results: user system total real inject(0) 20.320000 5.430000 25.750000 ( 34.486938) inject 20.450000 5.350000 25.800000 ( 30.152165) inject &:+ 37.860000 5.690000 43.550000 ( 45.670349) each 11.130000 3.460000 14.590000 ( 15.042142) C 0.020000 0.000000 0.020000 ( 0.019815) so inject(0) vs inject doesn't really matter. code: http://pastie.caboo.se/11534 (and yes i know the C code doesn't handle bignums) @Robert: you mean the thread "for performance, write in C" ? ;)