Last active
August 26, 2015 15:43
-
-
Save plukevdh/eb60b25d8929e372a7a4 to your computer and use it in GitHub Desktop.
Call comparisons
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
class Processor | |
def plus_two(arg) | |
arg + 2 | |
end | |
end | |
class Runner | |
def block_run(arg, &block) | |
yield arg | |
end | |
def sender(arg, cls, mth) | |
cls.send mth, arg | |
end | |
end | |
lambda = -> (arg) { arg + 2 } | |
proc = Proc.new {|arg| arg + 2 } | |
processor = Processor.new | |
runner = Runner.new | |
meth = processor.method(:plus_two) | |
rebinder = Processor.new | |
rebound = meth.unbind.bind rebinder | |
Benchmark.ips do |test| | |
test.report("Direct calls") { processor.plus_two(2) } | |
test.report("Via send") { runner.sender(2, processor, :plus_two) } | |
test.report("Blocks") { runner.block_run(2) {|arg| arg + 2 } } | |
test.report("Lambda") { runner.block_run(2, &lambda) } | |
test.report("Proc") { runner.block_run(2, &proc) } | |
test.report("Method") { runner.block_run(2, &meth) } | |
test.report("Rebound direct call") { rebinder.plus_two(2) } | |
test.report("Rebound method") { runner.block_run(2, &rebound) } | |
test.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
Calculating ------------------------------------- | |
Direct calls 135.058k i/100ms | |
Via send 125.582k i/100ms | |
Blocks 79.429k i/100ms | |
Lambda 129.758k i/100ms | |
Proc 122.336k i/100ms | |
Method 60.091k i/100ms | |
Rebound direct call 139.934k i/100ms | |
Rebound method 59.337k i/100ms | |
------------------------------------------------- | |
Direct calls 7.723M (± 6.9%) i/s - 38.492M | |
Via send 5.190M (± 6.6%) i/s - 25.870M | |
Blocks 1.515M (± 7.4%) i/s - 7.546M | |
Lambda 5.252M (± 7.2%) i/s - 26.211M | |
Proc 5.446M (± 5.3%) i/s - 27.159M | |
Method 998.451k (± 6.2%) i/s - 4.988M | |
Rebound direct call 8.097M (± 5.7%) i/s - 40.441M | |
Rebound method 1.018M (± 5.7%) i/s - 5.103M | |
Comparison: | |
Rebound direct call: 8096886.8 i/s | |
Direct calls: 7722969.7 i/s - 1.05x slower | |
Proc: 5445994.0 i/s - 1.49x slower | |
Lambda: 5251876.4 i/s - 1.54x slower | |
Via send: 5190066.6 i/s - 1.56x slower | |
Blocks: 1515285.7 i/s - 5.34x slower | |
Rebound method: 1017916.6 i/s - 7.95x slower | |
Method: 998451.0 i/s - 8.11x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment