Skip to content

Commit

Permalink
Add normal way to apply method to block element versus &method(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanitoFatas committed Jul 7, 2015
1 parent da3e53c commit cf64ab7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,42 @@ Comparison:
method_missing: 2728893.0 i/s - 1.40x slower
```

##### Normal way to apply method vs `&method(...)` [code](code/general/block-apply-method.rb)

```
$ ruby -v code/general/block-apply-method.rb
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
Calculating -------------------------------------
normal 85.749k i/100ms
&method 35.529k i/100ms
-------------------------------------------------
normal 1.867M (± 7.6%) i/s - 9.347M
&method 467.095k (± 6.4%) i/s - 2.345M
Comparison:
normal: 1866669.5 i/s
&method: 467095.4 i/s - 4.00x slower
```

##### `define_method` vs `module_eval` for Defining Methods [code](code/general/define_method-vs-module-eval.rb)

```
$ ruby -v code/general/define_method-vs-module-eval.rb
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
Calculating -------------------------------------
module_eval with string 125.000 i/100ms
define_method 138.000 i/100ms
-------------------------------------------------
module_eval with string 1.130k (±20.3%) i/s - 5.500k
define_method 1.346k (±25.9%) i/s - 6.348k
Comparison:
define_method: 1345.6 i/s
module_eval with string: 1129.7 i/s - 1.19x slower
```


### Array

Expand Down
19 changes: 19 additions & 0 deletions code/general/block-apply-method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "benchmark/ips"

def do_something(n)
4*n + 2
end

def fast
[1, 2, 3].map { |n| do_something(n) }
end

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

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

2 comments on commit cf64ab7

@sshaw
Copy link
Contributor

@sshaw sshaw commented on cf64ab7 Jul 7, 2015

Choose a reason for hiding this comment

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

Haven't looked at the ruby source for the respected methods but how is this different than block-vs-to_proc? (Well, Method#to_proc returns a lambda and Symbol#to_proc returns a Proc).

I think it's somewhat obvious that slow would be slower as the test is comparing a method that calls 3 methods to a method that calls 2.

@JuanitoFatas
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haven't looked at the ruby source for the respected methods but how is this different than block-vs-to_proc? (Well, Method#to_proc returns a lambda and Symbol#to_proc returns a Proc).

This is about passing the collection's member to method, and not calling method on the collection's member.

I think it's somewhat obvious that slow would be slower as the test is comparing a method that calls 3 methods to a method that calls 2.

I agree. Also method(:do_something) gets an instance of Method class. I think this method object costs a lot, so slower.

I added this benchmark because people may use this syntax and think this is faster like the case you mentioned: block-vs-to_proc.

Please sign in to comment.