Skip to content

Instantly share code, notes, and snippets.

@jacobdam
Forked from JoshTGreenwood/bench.rb
Last active April 8, 2016 05:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jacobdam/5864997 to your computer and use it in GitHub Desktop.
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
$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)
@nullstyle
Copy link

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:

  x.report('Break') do
    1_000_000.times do
      loop{ break }
    end
  end

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.

➜  tmp  ruby --version
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.2.1]
➜  tmp  ruby catchbench.rb
Rehearsal -------------------------------------------------------------
Break                       0.300000   0.000000   0.300000 (  0.299011)
Catch/Throw                 0.400000   0.000000   0.400000 (  0.401259)
Raise/Rescue                1.460000   0.020000   1.480000 (  1.468706)
Raise/Rescue no backtrace   1.130000   0.000000   1.130000 (  1.130255)
---------------------------------------------------- total: 3.310000sec

                                user     system      total        real
Break                       0.300000   0.000000   0.300000 (  0.296983)
Catch/Throw                 0.390000   0.000000   0.390000 (  0.395838)
Raise/Rescue                1.450000   0.000000   1.450000 (  1.450375)
Raise/Rescue no backtrace   1.130000   0.000000   1.130000 (  1.126825)

@jacobdam
Copy link
Author

jacobdam commented Apr 8, 2016

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