fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

&method(...) section misleading #70

Closed amencarini closed 6 years ago

amencarini commented 9 years ago

I found the &method(...) section a bit misleading, as I never found myself writing code that way, but alarmed me as I use Symbol#to_proc a lot. So I ran a different benchmark:

require "benchmark/ips"

def fast
  [1, 2, 3].map { |n| n.to_s }
end

def slow
  [1, 2, 3].map(&:to_s)
end

Benchmark.ips do |x|
  x.report("normal")  { fast }
  x.report("&method") { slow }
  x.compare!
end

Calculating -------------------------------------
              normal    59.170k i/100ms
             &method    57.531k i/100ms
-------------------------------------------------
              normal      1.118M (±11.5%) i/s -      5.562M
             &method      1.067M (±12.4%) i/s -      5.293M

Comparison:
              normal:  1118235.4 i/s
             &method:  1067078.5 i/s - 1.05x slower

Although I later found something targeting specifically to_proc I think it's worth making it clear that we're not talking about Symbol#to_proc syntax here. Thoughts?

JuanitoFatas commented 9 years ago

Hi! &method & Symbol#to_proc are different. Please see the explanations here: https://github.com/JuanitoFatas/fast-ruby/commit/cf64ab7e51bab12e4daf5956f2a7e93ace7665a4#commitcomment-12068179. Hope this helps.