-
-
Save jeremyevans/bd04784d3dbb93e1c0f11a0a547647ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
n = (ARGV[0] || 1).to_i | |
eval <<CODE | |
def bg | |
#{'a = block_given?;' * n} | |
a | |
end | |
def dy | |
#{'a = defined?(yield);' * n} | |
a | |
end | |
def b(&block) | |
#{'a = block;' * n} | |
a | |
end | |
CODE | |
block = proc{} | |
puts "With passed block" | |
Benchmark.ips do |x| | |
x.report("block_given?"){bg(&block)} | |
x.report("defined?(yield)"){dy(&block)} | |
x.report("&block"){b(&block)} | |
x.compare! | |
end | |
puts "With block" | |
Benchmark.ips do |x| | |
x.report("block_given?"){bg{}} | |
x.report("defined?(yield)"){dy{}} | |
x.report("&block"){b{}} | |
x.compare! | |
end | |
puts "Without block" | |
Benchmark.ips do |x| | |
x.report("block_given?"){bg} | |
x.report("defined?(yield)"){dy} | |
x.report("&block"){b} | |
x.compare! | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
With passed block | |
Warming up -------------------------------------- | |
block_given? 247.543k i/100ms | |
defined?(yield) 309.646k i/100ms | |
&block 282.631k i/100ms | |
Calculating ------------------------------------- | |
block_given? 2.477M (_ 0.3%) i/s - 12.625M in 5.095886s | |
defined?(yield) 3.098M (_ 0.3%) i/s - 15.792M in 5.096976s | |
&block 2.814M (_ 0.1%) i/s - 14.132M in 5.021455s | |
Comparison: | |
defined?(yield): 3098321.7 i/s | |
&block: 2814235.3 i/s - 1.10x (_ 0.00) slower | |
block_given?: 2477458.0 i/s - 1.25x (_ 0.00) slower | |
With block | |
Warming up -------------------------------------- | |
block_given? 282.683k i/100ms | |
defined?(yield) 350.667k i/100ms | |
&block 71.141k i/100ms | |
Calculating ------------------------------------- | |
block_given? 2.826M (_ 0.1%) i/s - 14.134M in 5.002070s | |
defined?(yield) 3.552M (_ 0.1%) i/s - 17.884M in 5.034722s | |
&block 712.031k (_ 1.3%) i/s - 3.628M in 5.096509s | |
Comparison: | |
defined?(yield): 3552137.7 i/s | |
block_given?: 2825663.9 i/s - 1.26x (_ 0.00) slower | |
&block: 712031.2 i/s - 4.99x (_ 0.00) slower | |
Without block | |
Warming up -------------------------------------- | |
block_given? 286.115k i/100ms | |
defined?(yield) 396.456k i/100ms | |
&block 330.508k i/100ms | |
Calculating ------------------------------------- | |
block_given? 2.865M (_ 0.2%) i/s - 14.592M in 5.093600s | |
defined?(yield) 4.005M (_ 0.1%) i/s - 20.219M in 5.048443s | |
&block 3.321M (_ 0.1%) i/s - 16.856M in 5.074991s | |
Comparison: | |
defined?(yield): 4005051.5 i/s | |
&block: 3321368.6 i/s - 1.21x (_ 0.00) slower | |
block_given?: 2864754.3 i/s - 1.40x (_ 0.00) slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment