Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Benchmark break vs. catch-throw vs. begin-rescue-end in ruby
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
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
Something went wrong with that request. Please try again.