Skip to content

Instantly share code, notes, and snippets.

Created August 6, 2010 05:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
ruby #inject vs. #sum
data = (1..1_000).to_a
measure "sum" do
data.sum { |n| n }
measure "inject" do
data.inject(0) { |sum, element| sum + element}
Date: August 06, 2010
System Information
Operating System: Mac OS X 10.6.4 (10F569)
CPU: Intel Core i7 2.66 GHz
Processor Count: 2
Memory: 4 GB
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.4.0]
"inject" is up to 48% faster over 1,000 repetitions
inject 0.0993571281433105 secs Fastest
sum 0.194322109222412 secs 48% Slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment