From the course: Faster Python Code

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Overview

Overview - Python Tutorial

From the course: Faster Python Code

Start my 1-month free trial

Overview

- [Instructor] Caching is the act of saving computation results and reusing them instead of running the calculations again. It is very common practice and will speed up your code in many scenarios. The price you pay is the memory used to hold the computed values. This runtime versus memory trade off is very common in computer science. Let's see an example. The Fibonacci sequence is defined as the first two values of one and every value after it is the sum of the two values before it. In fib.py, we have an implementation which is a cause. If the number in line six is smaller than two, we return one otherwise we return Fibonacci of N minus one plus Fibonacci of N minus two. Let's time it. So, ipython and then we run fib.py. And then, let's run timeit of fib of 20. So, it's 2.82 milliseconds. This implementation is slow since we do a lot of repeated calculation. When we compute Fibonacci of four, we'll call…

Contents