Another look at algorithm time complexity
In my last post I wrote about various Fibonacci implementations in Elixir. I timed each implementation generating a list of the first N Fibonacci numbers, and compared the performance characteristics of each implementation. Based on what unwind said on Lobsters in response I decided to revisit Fibonacci implementations in Elixir. I’m going to update the implementatios I used in my last blog post so they use Erlang processes to store previously computed Fibonacci numbers. I’ll then benchmark each implementation against the Fibonacci implementation to see how much processes sped things up.
Rewriting the Algorithms
I needed to have a process for each algorithm to store the computed Fibonacci numbers. I chose to create a single generic GenServer that I could spawn for each algorithm I was testing. Below is the implementation I settled on. It’s fairly straightforward. The only thing special about this GenServer code is that it allows the caller to specify the server they want to use. This is necessary because I will have multiple processes running different instances of this GenServer. One for each implementation I am testing. I need to have a different
FibStore server because each algorithm is different so I need to make sure I store and fetch Fibonacci numbers from the correct server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
Next I needed to update the existing functions to use this new GenServer for fetching and storing values. The previous implementations had a
fib function that performed the actual computation. To avoid modifying a lot of code I added a new function named
do_fib that only calls the existing
fib function to do the computation if the Fibonacci number has not already been computed and stored in the GenServer instance. Below are the three new implementations:
In my updated implementation so that the
fib clause for generating any number after the first two in the Fibonacci sequence invokes
FibStore.do_fib/3. This ensures it will reuse already an computed number if there is one. Otherwise
FibStore.do_fib/3 will invoke
fib/1 to compute and store the number.
1 2 3 4 5 6 7 8 9 10 11 12 13
In much the same way I changed the Rosetta Code algorithm. It invokes
FibStore.do_fib/3 when computing Fibonacci numbers so it uses pre-computed numbers if exist in the
FibStore process. Otherwise it invokes
fib/1 to compute the Fibonacci number. Note that unlike my implementation this one must recompute everything when generating a new number in the sequence. For example, when generating the fifth number it would not be able to reuse the
2 and the
3 cached in the
FibStore process. I could not make it reuse pre-computed numbers without changing the way the
fib/3 function works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Popular Implementation/Dave Thomas
This implementation benefited the most from the
FibStore caching of pre-computed numbers. It remains similar to the original implementation, but now it invokes
FibStore.do_fib/3 when computing numbers. Due to the recursive nature of the
fib/1 function, calls are made to
FibStore.do_fib/3 for every number that must be computed. This implementation is able to use the pre-computed numbers as a starting point when computing new numbers. Unlike the Rosetta Code implementation which cannot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Which function generates a list of Fibonacci numbers the fastest?
My bet was that my implementation would still perform better than the others, but I wasn’t sure which of the others would be the fastest. The Rosetta Code algorithm wasn’t able to leverage the
FibStore caching as much as others; but then again it was already a fairly fast algorithm.
I reused my benchmarking code from the first blog post. See the source file for the benchmarking code.
Since the new algorithms now maintain state their performance on the first run will differ from their performance on all subsequent runs. I decided I would run the benchmarks once to capture the performance on initial runs. Then I would run the benchmarks four more times to measure the performance on subsequent runs just as I did in my first blog post.
I ran the benchmark once to capture the performance when no state. Time is in microseconds.
|Number||Rosetta Code||Dave Thomas’
Four Subsequent Runs
After the first run I ran the benchmark again against each algorithm four times. The average run times are shown in the table below in microseconds.
|Number||Rosetta Code||Dave Thomas’
For comparison here are the average run times of the original algorithms:
|List Size||Rosetta Code||Dave Thomas’||Mine|
Looking at the data in these tables it’s clear the Dave Thomas algorithm benefited the most from the new process caching of computed numbers. This isn’t surprising due to the increasing number of recursive calls the original algorithm used when computing large numbers. It’s clear from the run times that the time complexity of the original algorithm was exponential. With the process caching in place the time complexity is no longer exponential. I didn’t take the time to figure it out, but I would guess the new Dave Thomas’ algorithm with process caching has linear time complexity.
The Rosetta Code algorithm doesn’t really benefit from the new process caching. The performance characteristics remain the same, and it runs nearly 5 times slower. My algorithm did not benefit from the process caching either. It ran about 4 times slower than the original algorithm. Even though processes are cheap on the Erlang VM, and local messages are fast it’s clear the overhead of caching the data in a separate process is taking a toll on these algorithms. Sending a message is cheap, but eventually the time it takes for the process to be scheduled after receiving a message adds up. When no data is cached these algorithms must send and receive at least 2 messages for every recursion needed to compute the final number. Even when the number has already been computed two messages are needed to fetch the number. The two original algorithms performed well by keep pre-computed numbers in process memory and reusing them when necessary, so this caching only reduces the arithmetic operations needed on subsequent calls. And even on subsequent calls the overhead of sending and receiving two messages is greater than the cost of computing the list of Fibonacci numbers all over again; at least for the first 45 Fibonacci numbers in the sequence.
When it comes to generating a list of the first N Fibonacci numbers my original algorithm still seems to be the fastest out of all the algorithms I’ve tested. My algorithm was designed from the ground up for specifically for generating a list of the first N Fibonacci numbers in the sequence so I think the take away here is that code written for a specific task may outperform code that is more general purpose.