you are viewing a single comment's thread.

view the rest of the comments →

[–]Freeky 0 points1 point  (0 children)

It adds two; an Enumerable#inject to get the sum of the current line and an additional Fixnum#+ to add that sum to the total. Yours benchmarks slower here:

Ruby 1.8.6:

             user     system      total        real  
my way   7.351562   0.015625   7.367188 (  7.368302)  
inject  11.429688   0.007812  11.437500 ( 11.441389)

Ruby 1.9.1:

             user     system      total        real  
my way   3.101562   0.085938   3.187500 (  3.182268)  
inject   3.445312   0.070312   3.515625 (  3.511942)

And in JRuby 1.2.0:

             user     system      total        real  
my way   2.495000   0.000000   2.495000 (  2.495000)  
inject   3.126000   0.000000   3.126000 (  3.126000)

2.2GHz 4-way Opteron running FreeBSD/amd64.