-
-
Save gingray/df2761dea78386701fd4a5aae263e0b8 to your computer and use it in GitHub Desktop.
Benchmark break vs. catch-throw vs. begin-rescue-end in ruby
This file contains 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' | |
Benchmark.bmbm do |x| | |
x.report('Break') do | |
1_000_000.times do | |
break | |
end | |
end | |
x.report('Catch/Throw') do | |
1_000_000.times do | |
catch(:benchmarking) do | |
throw(:benchmarking) | |
end | |
end | |
end | |
x.report('Raise/Rescue') do | |
1_000_000.times do | |
begin | |
raise StandardError | |
rescue | |
# do nothing | |
end | |
end | |
end | |
end |
This file contains 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
Rehearsal ------------------------------------------------ | |
Break 0.000000 0.000000 0.000000 ( 0.000017) | |
Catch/Throw 0.760000 0.300000 1.060000 ( 1.056713) | |
Raise/Rescue 10.390000 0.590000 10.980000 ( 11.017974) | |
-------------------------------------- total: 12.040000sec | |
user system total real | |
Break 0.000000 0.000000 0.000000 ( 0.000005) | |
Catch/Throw 0.770000 0.290000 1.060000 ( 1.064175) | |
Raise/Rescue 9.630000 0.580000 10.210000 ( 10.232384) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment