Skip to content

Instantly share code, notes, and snippets.

@JoshTGreenwood
Created November 21, 2012 00:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JoshTGreenwood/4122195 to your computer and use it in GitHub Desktop.
Save JoshTGreenwood/4122195 to your computer and use it in GitHub Desktop.
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)
@douglasresende
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment