Improving performance

We'll look at two performance tweaks for the Power1 class shown previously.

First, we need to switch to a better algorithm. Then, we will require a better algorithm combined with memoization, which involves a cache; therefore, the function becomes stateful. This is where callable objects shine.

The first modification is to use a divide-and-conquer design strategy. The previous version chopped the computation of into steps; the loop carried out n individual multiplication operations. If we can find a way to split the problem ...

Get Mastering Object-Oriented Python - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.