Last active
August 29, 2015 14:10
-
-
Save KamilLelonek/2794aee8c20ce04b49b6 to your computer and use it in GitHub Desktop.
Exceptions benchmark
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' | |
def rescue_exception(divider) | |
1 / divider | |
rescue ZeroDivisionError | |
end | |
def without_rescue(divider) | |
1 / divider | |
end | |
Benchmark.ips do |x| | |
x.report('rescue') do |t| | |
rescue_exception t | |
end | |
x.report('regular') do |t| | |
without_rescue t | |
end | |
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
Calculating ------------------------------------- | |
rescue 141.144k i/100ms | |
regular 143.577k i/100ms | |
------------------------------------------------- | |
rescue 142.007B (±17.3%) i/s - 182.810B | |
regular 144.436B (±17.4%) i/s - 183.715B | |
Comparison: | |
regular: 144435990041.5 i/s | |
rescue: 142006866116.1 i/s - 1.02x slower | |
[Finished in 15.8s] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code suggest that you run in benchmark the same method twice (rescue_exception).