Skip to content

Instantly share code, notes, and snippets.

@caius
Created November 5, 2018 23:26
Show Gist options
  • Save caius/966c6e7e333a4dafdad619d89e0452b6 to your computer and use it in GitHub Desktop.
Save caius/966c6e7e333a4dafdad619d89e0452b6 to your computer and use it in GitHub Desktop.
require "benchmark/ips"
Benchmark.ips do |r|
r.report("block argument, not yielded") do
m = Module.new do
def foo(&block)
end
module_function :foo
end
m.foo do
end
end
r.report("no argument, not yielded") do
m = Module.new do
def foo
end
module_function :foo
end
m.foo do
end
end
r.report("block argument, yielded") do
m = Module.new do
def foo(&block)
block.call
end
module_function :foo
end
m.foo do
end
end
r.report("no argument, yielded") do
m = Module.new do
def foo
yield
end
module_function :foo
end
m.foo do
end
end
r.report("block argument, no block given") do
m = Module.new do
def foo(&block)
end
module_function :foo
end
m.foo
end
r.report("no argument, no block given") do
m = Module.new do
def foo
end
module_function :foo
end
m.foo
end
end
__END__
Warming up --------------------------------------
block argument, not yielded 40.191k i/100ms
no argument, not yielded 41.194k i/100ms
block argument, yielded 35.276k i/100ms
no argument, yielded 41.049k i/100ms
block argument, no block given 41.113k i/100ms
no argument, no block given 41.267k i/100ms
Calculating -------------------------------------
block argument, not yielded 448.670k (± 1.8%) i/s - 2.251M in 5.018106s
no argument, not yielded 438.739k (± 7.3%) i/s - 2.183M in 5.007818s
block argument, yielded 367.827k (± 5.4%) i/s - 1.834M in 5.003140s
no argument, yielded 445.064k (± 2.1%) i/s - 2.258M in 5.074977s
block argument, no block given 446.703k (± 3.2%) i/s - 2.261M in 5.067838s
no argument, no block given 448.327k (± 2.9%) i/s - 2.270M in 5.067136s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment