Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6661321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:15:55+00:00 2026-05-26T02:15:55+00:00

b1 = Time.now puts (1..100000).inject(0) { |x, y| x + y } a1 =

  • 0
b1 = Time.now
puts (1..100000).inject(0) { |x, y| x + y }
a1 = Time.now
puts "Time for inject: #{a1 - b1}"

b2 = Time.now
sum = 0
(1..100000).each do |value|
    sum += value
end
puts sum
a2 = Time.now
puts "Time for each: #{a2 - b2}"

The above Ruby code compares two ways of summing up integers. To my surprise, the more elegant inject or reduce approach is outperformed by the other. Why is that the case? Why do people bother using the inefficient inject or reduce? Simply because it’s elegant?

PS: Thanks for all the inspiring answers. My intention was to ask what’s going on behind the scene that results in the differences.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:15:55+00:00Added an answer on May 26, 2026 at 2:15 am

    I would go with a little math in this case:

    require "benchmark"
    
    N = 5_000_000
    
    Benchmark.bmbm do |bm|
      bm.report "inject 1" do
        (1..N).inject(0) { |x, y| x + y }
      end
    
      bm.report "inject 2" do
        (1..N).inject(:+)
      end
    
      bm.report "each" do
        sum = 0
        (1..N).each do |value|
          sum += value
        end
      end
    
      bm.report "sum of finite arithmetic progression" do
        ((1 + N) * N) / 2
      end
    end
    

    And the result is:

    % ruby sum.rb
    Rehearsal ------------------------------------------------------------------------
    inject 1                               0.500000   0.000000   0.500000 (  0.507497)
    inject 2                               0.320000   0.000000   0.320000 (  0.322675)
    each                                   0.370000   0.000000   0.370000 (  0.380504)
    sum of finite arithmetic progression   0.000000   0.000000   0.000000 (  0.000005)
    --------------------------------------------------------------- total: 1.190000sec
    
                                               user     system      total        real
    inject 1                               0.500000   0.000000   0.500000 (  0.507697)
    inject 2                               0.320000   0.000000   0.320000 (  0.322323)
    each                                   0.370000   0.000000   0.370000 (  0.380307)
    sum of finite arithmetic progression   0.000000   0.000000   0.000000 (  0.000004)
    % 
    

    A better math is always faster 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

t = Time.now d = 10.minutes.from_now Using these two variables, how do I output
I have this trial timer code to time euler solutions in Ruby. $RUNS =
I'm playing with Ruby EventMachines for some time now and I think I'm understandings
For some time now, I've been using the design pattern of objectOne, shown below.
I have spent some time now to solve a problem for which i have
For the longest time now I've been trying to convert HTML pages containing large
For a long time now I have noticed something annoying when working on Web
for the longest time now i've used object literals to replace multiple var statements
I've been programming OpenGL for some time now but I'm quite new to OpenGL
I've been using NSViewAnimation for some time now and I've read somewhere that Core

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.