Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Created October 21, 2018 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgaskins/cf1a79b37617c12ad6e8912cd9821974 to your computer and use it in GitHub Desktop.
Save jgaskins/cf1a79b37617c12ad6e8912cd9821974 to your computer and use it in GitHub Desktop.
Benchmarking conditionals vs exceptions
require 'benchmark/ips'
def expensive_operation duration=0.001
sleep duration
false
end
Benchmark.ips do |x|
x.report 'simple if' do
if false
whatever
else
false
end
end
x.report 'simple exception' do
begin
if false
whatever
else
raise ArgumentError, "omg"
end
rescue ArgumentError => e
end
end
x.compare!
end
Benchmark.ips do |x|
x.report 'expensive if' do
if expensive_operation
whatever
else
false
end
end
x.report 'expensive exception' do
begin
if expensive_operation
whatever
else
raise ArgumentError, "omg"
end
rescue ArgumentError => e
end
end
x.compare!
end

Microbenchmark shows conditionals are wildly faster:

Warming up --------------------------------------
           simple if   416.263k i/100ms
    simple exception   105.397k i/100ms
Calculating -------------------------------------
           simple if     16.148M (± 1.5%) i/s -     80.755M in   5.002331s
    simple exception      1.298M (± 1.8%) i/s -      6.535M in   5.034078s

Comparison:
           simple if: 16147688.0 i/s
    simple exception:  1298493.6 i/s - 12.44x  slower

This looks terrible. The example without the exception is over 12x as fast. We could do 15 MILLION more iterations per second if not for the exception handling!

In the "expensive" example, we add 1 millisecond of sleep to simulate a small level of work. In a request to a Rails app, you're almost guaranteed to be spending more than 1ms (one DB query alone is likely to blow past that), but this is just to illustrate the effect that even a tiny amount of work has on that 12x factor we saw above. Have a gander at the result:

Warming up --------------------------------------
        expensive if    76.000  i/100ms
 expensive exception    74.000  i/100ms
Calculating -------------------------------------
        expensive if    741.318  (± 1.6%) i/s -      3.724k in   5.024683s
 expensive exception    744.120  (± 3.2%) i/s -      3.774k in   5.076659s

Comparison:
 expensive exception:      744.1 i/s
        expensive if:      741.3 i/s - same-ish: difference falls within error

The difference disappears completely. The exception resulted in virtually no performance penalty.

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