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

Add test for ARRAY.inject(:+) vs ARRAY.sum #140

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jrmhaig
Copy link

@jrmhaig jrmhaig commented Dec 23, 2017

I was curious to see the difference between the sum method in enumerables, which was introduced in Ruby 2.4, and the commonly used inject(:+). For me inject(:+) is a little bit slower (1.02x).

This PR cannot be merged immediately as it will break for Ruby < 2.4. Do you have a way to only run tests for supported versions of Ruby?

@jrmhaig
Copy link
Author

jrmhaig commented Dec 24, 2017

I am surprised to see that this passed in Travis CI even though, in the log with Ruby 2.3.0, there are errors:

$ ruby -v code/enumerable/inject-symbol-plus-vs-sum.rb
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
Warming up --------------------------------------
                 sumcode/enumerable/inject-symbol-plus-vs-sum.rb:7:in `fast': undefined method `sum' for #<Array:0x000000023deee0> (NoMethodError)
	from code/enumerable/inject-symbol-plus-vs-sum.rb:15:in `block (2 levels) in <main>'
	from /home/travis/build/JuanitoFatas/fast-ruby/vendor/bundle/ruby/2.3.0/gems/benchmark-ips-2.7.2/lib/benchmark/ips/job/entry.rb:53:in `call_times'
	from /home/travis/build/JuanitoFatas/fast-ruby/vendor/bundle/ruby/2.3.0/gems/benchmark-ips-2.7.2/lib/benchmark/ips/job.rb:220:in `block in run_warmup'
	from /home/travis/build/JuanitoFatas/fast-ruby/vendor/bundle/ruby/2.3.0/gems/benchmark-ips-2.7.2/lib/benchmark/ips/job.rb:206:in `each'
	from /home/travis/build/JuanitoFatas/fast-ruby/vendor/bundle/ruby/2.3.0/gems/benchmark-ips-2.7.2/lib/benchmark/ips/job.rb:206:in `run_warmup'
	from /home/travis/build/JuanitoFatas/fast-ruby/vendor/bundle/ruby/2.3.0/gems/benchmark-ips-2.7.2/lib/benchmark/ips/job.rb:185:in `block in run'
	from /home/travis/build/JuanitoFatas/fast-ruby/vendor/bundle/ruby/2.3.0/gems/benchmark-ips-2.7.2/lib/benchmark/ips/job.rb:184:in `times'
	from /home/travis/build/JuanitoFatas/fast-ruby/vendor/bundle/ruby/2.3.0/gems/benchmark-ips-2.7.2/lib/benchmark/ips/job.rb:184:in `run'
	from /home/travis/build/JuanitoFatas/fast-ruby/vendor/bundle/ruby/2.3.0/gems/benchmark-ips-2.7.2/lib/benchmark/ips.rb:59:in `ips'
	from code/enumerable/inject-symbol-plus-vs-sum.rb:14:in `<main>'

Is this intended?

I expected this error as ARRAY.sum will only work in 2.4.0+ but I also expected this to cause Travis to fail.

@kbrock
Copy link
Contributor

kbrock commented Apr 27, 2018

maybe add a if RUBY > "2.4.0" ?

but I'm not a committer, so not sure the official way we do things here

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