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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.