Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass a symbol instead of a block in Enumerable#sum #4471

Merged
merged 1 commit into from
Jan 15, 2012

Conversation

semaperepelitsa
Copy link
Contributor

This is faster and more concise. At least Ruby 1.8.7 is required which is ok since 3.2.

Benchmark:

require "benchmark"

enum = 1..10_000
N = 100
Benchmark.bm do |x|
  x.report "inject block" do
    N.times do
      enum.inject { |sum, n| sum + n }
    end
  end

  x.report "inject symbol" do
    N.times do
      enum.inject(:+)
    end
  end
end

Result:

       user     system      total        real
inject block   0.160000   0.000000   0.160000 (  0.179723)
inject symbol  0.090000   0.000000   0.090000 (  0.095667)

At least Ruby 1.8.7 is required which is ok since 3.2.

Benchmark:

```ruby
require "benchmark"

enum = 1..10_000
N = 100
Benchmark.bm do |x|
  x.report "inject block" do
    N.times do
      enum.inject { |sum, n| sum + n }
    end
  end

  x.report "inject symbol" do
    N.times do
      enum.inject(:+)
    end
  end
end
```

Result:

```
       user     system      total        real
inject block   0.160000   0.000000   0.160000 (  0.179723)
inject symbol  0.090000   0.000000   0.090000 (  0.095667)
```
tenderlove added a commit that referenced this pull request Jan 15, 2012
Pass a symbol instead of a block in Enumerable#sum
@tenderlove tenderlove merged commit fce3afa into rails:3-2-stable Jan 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants