diff options
author | claudiob <claudiob@gmail.com> | 2014-10-17 11:38:00 -0700 |
---|---|---|
committer | claudiob <claudiob@gmail.com> | 2014-10-17 11:46:37 -0700 |
commit | 6aa115e41f43b179d4c74526aa250d01e823d48d (patch) | |
tree | e0d4408b6a5b1a0e53c524688e9a38ae687c3e62 /activerecord/test | |
parent | 8a80e3b6c446931330ad07130603712e8d0a2ff3 (diff) | |
download | rails-6aa115e41f43b179d4c74526aa250d01e823d48d.tar.gz rails-6aa115e41f43b179d4c74526aa250d01e823d48d.tar.bz2 rails-6aa115e41f43b179d4c74526aa250d01e823d48d.zip |
Replace (slower) block.call with (faster) yield
Performance optimization: `yield` with an implicit `block` is faster than `block.call`.
See http://youtu.be/fGFM_UrSp70?t=10m35s and the following benchmark:
```ruby
require 'benchmark/ips'
def fast
yield
end
def slow(&block)
block.call
end
Benchmark.ips do |x|
x.report('fast') { fast{} }
x.report('slow') { slow{} }
end
# => fast 154095 i/100ms
# => slow 71454 i/100ms
# =>
# => fast 7511067.8 (±5.0%) i/s - 37445085 in 4.999660s
# => slow 1227576.9 (±6.8%) i/s - 6145044 in 5.028356s
```
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/migrator_test.rb | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/activerecord/test/cases/migrator_test.rb b/activerecord/test/cases/migrator_test.rb index 9809d83315..f05ca900aa 100644 --- a/activerecord/test/cases/migrator_test.rb +++ b/activerecord/test/cases/migrator_test.rb @@ -357,11 +357,11 @@ class MigratorTest < ActiveRecord::TestCase end private - def m(name, version, &block) + def m(name, version) x = Sensor.new name, version x.extend(Module.new { - define_method(:up) { block.call(:up, x); super() } - define_method(:down) { block.call(:down, x); super() } + define_method(:up) { yield(:up, x); super() } + define_method(:down) { yield(:down, x); super() } }) if block_given? end |