Skip to content

Commit a55d178

Browse files
committed
Merge pull request #68 from schmurfy/patch-1
yield vs block.call misleading
2 parents 1a991b9 + 5cf5e0f commit a55d178

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -640,19 +640,20 @@ Comparison:
640640
##### `Proc#call` vs `yield` [code](code/proc-and-block/proc-call-vs-yield.rb)
641641

642642
```
643-
$ ruby -v code/proc-and-block/proc-call-vs-yield.rb
644-
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
645-
643+
$ ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin13]
646644
Calculating -------------------------------------
647-
block.call 70.663k i/100ms
648-
yield 125.061k i/100ms
645+
block.call 55.809k i/100ms
646+
block + yield 56.025k i/100ms
647+
yield 89.387k i/100ms
649648
-------------------------------------------------
650-
block.call 1.309M (± 5.7%) i/s - 6.572M
651-
yield 6.103M (± 7.7%) i/s - 30.390M
649+
block.call 1.069M (±21.2%) i/s - 4.967M
650+
block + yield 1.172M (±16.8%) i/s - 5.715M
651+
yield 4.087M (±11.0%) i/s - 20.201M
652652
653653
Comparison:
654-
yield: 6102822.9 i/s
655-
block.call: 1309452.1 i/s - 4.66x slower
654+
yield: 4087005.2 i/s
655+
block + yield: 1171912.2 i/s - 3.49x slower
656+
block.call: 1068741.4 i/s - 3.82x slower
656657
```
657658

658659

code/proc-and-block/proc-call-vs-yield.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ def slow &block
44
block.call
55
end
66

7+
def slow2 &block
8+
yield
9+
end
10+
711
def fast
812
yield
913
end
1014

1115
Benchmark.ips do |x|
1216
x.report('block.call') { slow { 1 + 1 } }
17+
x.report('block + yield') { slow2 { 1 + 1 } }
1318
x.report('yield') { fast { 1 + 1 } }
1419
x.compare!
1520
end

0 commit comments

Comments
 (0)