-
-
Save jacobdam/5864997 to your computer and use it in GitHub Desktop.
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 | |
loop { 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 | |
x.report('Raise/Rescue no backtrace') do | |
empty_backtrace = [] | |
1_000_000.times do | |
begin | |
raise StandardError, '', empty_backtrace | |
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
$rvm use 2.0.0 exec ruby bench.rb | |
Rehearsal ------------------------------------------------------------- | |
Break 0.270000 0.000000 0.270000 ( 0.269271) | |
Catch/Throw 0.360000 0.000000 0.360000 ( 0.366416) | |
Raise/Rescue 1.260000 0.060000 1.320000 ( 1.310333) | |
Raise/Rescue no backtrace 0.880000 0.000000 0.880000 ( 0.890402) | |
---------------------------------------------------- total: 2.830000sec | |
user system total real | |
Break 0.260000 0.000000 0.260000 ( 0.261591) | |
Catch/Throw 0.360000 0.000000 0.360000 ( 0.365446) | |
Raise/Rescue 1.210000 0.040000 1.250000 ( 1.245223) | |
Raise/Rescue no backtrace 0.890000 0.000000 0.890000 ( 0.892867) | |
$rvm use 2.1.8 exec ruby bench.rb | |
Rehearsal ------------------------------------------------------------- | |
Break 0.260000 0.000000 0.260000 ( 0.266685) | |
Catch/Throw 0.370000 0.000000 0.370000 ( 0.374992) | |
Raise/Rescue 1.550000 0.030000 1.580000 ( 1.580658) | |
Raise/Rescue no backtrace 1.000000 0.010000 1.010000 ( 1.001632) | |
---------------------------------------------------- total: 3.220000sec | |
user system total real | |
Break 0.260000 0.000000 0.260000 ( 0.262560) | |
Catch/Throw 0.380000 0.000000 0.380000 ( 0.376881) | |
Raise/Rescue 1.500000 0.020000 1.520000 ( 1.528191) | |
Raise/Rescue no backtrace 0.990000 0.000000 0.990000 ( 0.986605) | |
$rvm use 2.2.4 exec ruby bench.rb | |
Rehearsal ------------------------------------------------------------- | |
Break 0.260000 0.000000 0.260000 ( 0.265079) | |
Catch/Throw 0.370000 0.000000 0.370000 ( 0.370250) | |
Raise/Rescue 1.390000 0.030000 1.420000 ( 1.421014) | |
Raise/Rescue no backtrace 0.940000 0.000000 0.940000 ( 0.946083) | |
---------------------------------------------------- total: 2.990000sec | |
user system total real | |
Break 0.270000 0.000000 0.270000 ( 0.268345) | |
Catch/Throw 0.370000 0.000000 0.370000 ( 0.370582) | |
Raise/Rescue 1.350000 0.020000 1.370000 ( 1.372187) | |
Raise/Rescue no backtrace 0.940000 0.010000 0.950000 ( 0.936232) | |
$rvm use 2.3.0 exec ruby bench.rb | |
Rehearsal ------------------------------------------------------------- | |
Break 0.240000 0.000000 0.240000 ( 0.235450) | |
Catch/Throw 0.320000 0.000000 0.320000 ( 0.322639) | |
Raise/Rescue 1.070000 0.020000 1.090000 ( 1.093540) | |
Raise/Rescue no backtrace 0.600000 0.000000 0.600000 ( 0.594695) | |
---------------------------------------------------- total: 2.250000sec | |
user system total real | |
Break 0.230000 0.000000 0.230000 ( 0.233382) | |
Catch/Throw 0.320000 0.000000 0.320000 ( 0.321296) | |
Raise/Rescue 1.060000 0.020000 1.080000 ( 1.069689) | |
Raise/Rescue no backtrace 0.590000 0.000000 0.590000 ( 0.586801) |
Thanks @nullstyle for your catch of "break" test. 👍 I've updated it and added more test results on recent ruby versions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a note, you're "break" test isn't really a fair test.
break
will actually kill the million iteration loop, meaning only a single break statement will be executed. An IMO more appropriate test would be something like:In this case you can see the catch/throw is only 1/3 slower than break, further illustrating that it's best to use for flow control.