Skip to content

Instantly share code, notes, and snippets.

@ngauthier
Forked from bcardarella/benchmark.rb
Created September 27, 2011 15:16
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ngauthier/1245342 to your computer and use it in GitHub Desktop.

Using Exceptions to manage control flow in Rails Controllers...

is very very slow

require 'benchmark'
def ifelse
val = rand(1000000) > 1
if val
1
else
2
end
end
def raiserescue
val = rand(1000000) > 1
begin
raise unless val
rescue
2
end
end
Benchmark.bm do |x|
x.report("if/else ") { 10_000_000.times { ifelse } }
x.report("raise/rescue") { 10_000_000.times { raiserescue } }
end
~ $ ruby benchmark.rb
user system total real
if/else 3.810000 0.000000 3.810000 ( 3.814801)
raise/rescue 3.940000 0.000000 3.940000 ( 3.935940)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment