Performance deep dive

Bob
Bob in Python

In this approach, we'll find out how to most efficiently determine the response for Bob in Python.

The approaches page lists two idiomatic approaches to this exercise:

  1. Using if statements.
  2. Using if statements nested.

For our performance investigation, we'll also include a third approach that uses an answer list.

Benchmarks

To benchmark the approaches, we wrote a small benchmark application using the timeit library.

if statements: 2.4691750000056346e-07
if statements nested: 2.3319310000078987e-07
answer list: 2.5469370000064373e-07

The nested if approach is fastest, but some may consider nested if statements a bit less readable than the unnested if statements. The answer list approach is slowest, but some may prefer doing away with the chain of if statements. Since the difference between them is a few nanoseconds, which one is used may be a matter of stylistic preference.

4th Sep 2024 · Found it useful?