Skip to content

Conversation

kbrock
Copy link
Contributor

@kbrock kbrock commented Apr 4, 2016

Someone had asked me about using the block short form vs manually entering the code.

Turns out, in 1.8.7, using the block was 1.7x slower, but since then, the symbol form is a little quicker.

Thanks for this repo

@@ -0,0 +1,19 @@
require "benchmark/ips"

User = Struct.new(:name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused variable
How about ARRAY.inject(:+) too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol. must have been a copy paste from another enumerable one. oops

@nateberkopec
Copy link
Collaborator

@nateberkopec nateberkopec reopened this Apr 4, 2016
@nateberkopec
Copy link
Collaborator

Whoops, didnt mean to unilaterally close it. My bad!

@kbrock kbrock force-pushed the inject-sum-vs-block branch from 5f15498 to fd0f34a Compare April 4, 2016 17:20
@kbrock
Copy link
Contributor Author

kbrock commented Apr 4, 2016

Made fixes.

Think @nateberkopec is right. that is covered.
Sorry I didn't see it.

agreed to close?

@Arcovion
Copy link
Collaborator

Arcovion commented Apr 4, 2016

Just tested, seems like even when 0 was passed the block ones were still exactly equal.
The symbol one is much faster though:

require "benchmark/ips"

ARRAY = (1..10_000_000).to_a

def fastest
  ARRAY.inject(:+)
end

def fast
  ARRAY.inject(&:+)
end

def slow
  ARRAY.inject { |a, i| a + i }
end

Benchmark.ips do |x|
  x.report('inject symbol') { fastest }
  x.report('inject block') { fast }
  x.report('inject sum')   { slow }

  x.compare!
end
Warming up --------------------------------------
       inject symbol     1.000  i/100ms
        inject block     1.000  i/100ms
          inject sum     1.000  i/100ms
Calculating -------------------------------------
       inject symbol      2.372  (± 0.0%) i/s -     12.000 
        inject block      1.668  (± 0.0%) i/s -      9.000  in   5.398692s
          inject sum      1.667  (± 0.0%) i/s -      9.000  in   5.408312s

Comparison:
       inject symbol:        2.4 i/s
        inject block:        1.7 i/s - 1.42x slower
          inject sum:        1.7 i/s - 1.42x slower

So I think it's worth having a benchmark to show this—even though it's very specific and only applies to #inject/#reduce.
You need to add the benchmark results to the README (I think it was there before, but not in your last commit?)

@kbrock
Copy link
Contributor Author

kbrock commented Apr 4, 2016

aah - symbol. forgot that one. Yea. there was a reason why I was visiting this code :)

@kbrock
Copy link
Contributor Author

kbrock commented Apr 4, 2016

@Arcovion what version of ruby are you running?
I've tried with small arrays and larger arrays, and they never show sum and block with the same values

for that matter, what version of ruby should I be using for the readme?

@Arcovion
Copy link
Collaborator

Arcovion commented Apr 4, 2016

ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
Doesn't matter what Ruby you use for the benchmark, no problem if the results differ.

@kbrock kbrock force-pushed the inject-sum-vs-block branch from fd0f34a to 17e494b Compare April 4, 2016 17:59
@kbrock
Copy link
Contributor Author

kbrock commented Apr 7, 2016

@Arcovion @nateberkopec Is this good for you? Any other changes?

@Arcovion Arcovion merged commit b7a8b74 into fastruby:master Apr 9, 2016
@Arcovion
Copy link
Collaborator

Arcovion commented Apr 9, 2016

Thanks, LGTM.

@kbrock kbrock deleted the inject-sum-vs-block branch April 9, 2016 23:06

ARRAY = (1..1000).to_a

def fastest
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think naming the methods after their relative speed is a bit presumptuous. They're subject to change across runtimes and versions. I think it'd be better to name them after the type of operation being performed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I was trying to follow the style guide but I agree

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry. I see now that that could be read as an attack. I just meant the convention in general should change, IMHO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol. we're good.

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.

4 participants