Empirical complexity

O(2ⁿ)·O(n) space
010203040501101001k10k100k1M10M100M1Gncalls
  • Naive Fibonacci

The axes are set: 15 sizes from 4 to 32, against a count that will reach 1,000,000,000. Nothing has been measured yet.

The axes are set: 15 sizes from 4 to 32, against a count that will reach 1,000,000,000. Nothing has been measured yet.
Fit so far—

Naive Fibonacci: O(2ⁿ), claimed as 2ⁿ

  1. 1set up the axes: the sizes to measure, and the counts they will reach
  2. 2take the next size, and build an input of that size in the chosen shape
  3. 3run the algorithm on it, counting one operation at a time
  4. 4plot the point, and refit the textbook bound to everything measured so far
  5. 5stop: report how well the bound fits, and what a handful of sizes cannot prove

What the measurement shows

Nothing has been measured yet. The ladder runs from 4 to 32, in 15 steps.

Sizes measured
0
Operations counted
0
Largest single count
0
Calls
0

Complexity

BestO(φⁿ)
AverageO(φⁿ)
WorstO(φⁿ)
Extra spaceO(n)

Naive Fibonacci

Every Fibonacci number is the sum of the two before it, and written as a direct recursion that means every call makes two more until it reaches a base case. Nothing is remembered, so fib(n−2) is computed once for the fib(n−1) branch and again for the fib(n−2) branch, and so on all the way down. The number of calls is exactly 2·fib(n+1) − 1, which grows by a factor of the golden ratio for every 1 added to n.

The claim being tested

O(2ⁿ)

When it is the right choice

Never, as an algorithm. It is here because it is the clearest exponential a reader can hold in their head, and because it is the standard example of a recursion that recomputes: the fix is either a memo or a table, and both are one page away in other tools. Its practical lesson is about the shape of the recursion tree rather than about Fibonacci, which is rarely the thing anyone actually needs.

How it works

Worth noticing

Cost

Best
O(φⁿ)
Average
O(φⁿ)
Worst
O(φⁿ)
Extra space
O(n)
n is
the argument to a recursion
Measures
shuffled: up to n = 32

The ceilings are a time limit rather than a memory one, which makes this tool the odd one out in the suite. Its recorded steps are tiny — one step per measured size, not one per operation — so nothing about the trace would ever stop a run. What has to be stopped is the measuring itself: a configuration that would take more than forty million operations to count is refused before a single one of them is counted, from a formula in the input sizes rather than by starting and giving up. Two further limits sit beside it that no formula in operations can see: building the input is real work, and a recursion n deep overflows the call stack long before its operation count becomes interesting.

A fit over a dozen sizes is evidence and not proof. It cannot rule out a term that only begins to matter past the largest size measured, and it says nothing at all about the constant factors that decide which of two algorithms in the same class is actually faster on a real machine.

Worth comparing

All measured subjects