Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Forked from ngauthier/readme.md
Created September 27, 2011 15:12
Show Gist options
  • Save bcardarella/1245327 to your computer and use it in GitHub Desktop.
Save bcardarella/1245327 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
if true
1
else
2
end
end
def raiserescue
begin
raise
rescue
2
end
end
Benchmark.bm do |x|
x.report("if/else ") { 10_000.times { ifelse } }
x.report("raise/rescue") { 10_000.times { raiserescue } }
end
~ $ ruby benchmark.rb
user system total real
if/else 0.000000 0.000000 0.000000 ( 0.000985)
raise/rescue 0.100000 0.000000 0.100000 ( 0.102185)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment