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

Improve performance using Array#sum #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tricknotes
Copy link
Contributor

@tricknotes tricknotes commented Nov 26, 2022

The following micro benchmark shows Array#sum is better than Array#map with Array#inject.

require 'benchmark'

array = 4.times.to_a

Benchmark.bm(24) {|x|
  x.report('Array#sum') do
    100000.times {
      array.sum(0, &:itself)
    }
  end

  x.report('Array#map & Array#inject') do
    100000.times {
      array.map(&:itself).inject(0, &:+)
    }
  end
}
$ ruby -v
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [arm64-darwin21]
$ ruby benchmard.rb
                               user     system      total        real
Array#sum                  0.014578   0.000027   0.014605 (  0.014607)
Array#map & Array#inject   0.040410   0.000191   0.040601 (  0.040623)

@yob
Copy link
Owner

yob commented Nov 26, 2022

Correct me if I'm wrong, but I believe Array#sum was introduced in ruby 2.4. Puma 5.0 supports ruby 2.2+, and puma 6.0 increases that minimum version to 2.4+.

I'm up for merging this, but would prefer to wait until we depend on puma 6 and can safely assume all users are on ruby 2.4+.

@tricknotes
Copy link
Contributor Author

Thanks for your reply, @yob.
Okay, I missed that puma supports older Ruby versions.
I'm keeping this PR as opened.

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.

2 participants