From: MenTaLguY Date: 2008-02-21T08:12:48+09:00 Subject: Re: IP Address to Decimal - inject sucks On Thu, 21 Feb 2008 07:10:25 +0900, Ryan Davis wrote: > On Feb 20, 2008, at 11:50 , Alex Shulgin wrote: >> Everybody replied already, still I think left-shift deserves >> mentioning: >> >> "127.0.0.1".split('.').inject(0) { |s,x| (s << 8) + x.to_i } >> => 2130706433 > > It does, and it is the only inject form I consider valid. However, > converting it to map leads to a 20-25% reduction in time. Since we aren't using map's result, why not each? # of iterations = 1000000 user system total real null_time 0.218000 0.000000 0.218000 ( 0.219000) split/each/<<+ 24.516000 0.140000 24.656000 ( 24.703000) split/map/<<+ 26.109000 0.172000 26.281000 ( 26.281000) split/inject/<<+ 30.797000 0.172000 30.969000 ( 31.000000) Being an FP nut used to using folds, I'm always a little annoyed that inject fares so poorly in these things; mainly we're just lacking a C implementation of Array#inject. -mental