How to do it...

  1. The following code (time.py) uses the Pythagorean Theorem to calculate the hypotenuse for a number of triangles with increasing side lengths:
        import math        TIMES = 10000000        a = 1        b = 1         for i in range(TIMES):            c = math.sqrt(math.pow(a, 2) + math.pow(b, 2))            a += 1            b += 2
  1. The following code (time2.py) does the same thing as pythag_theorem.py but puts the calculations within a function, rather than performing the calculation in line:
        import math        TIMES = 10000000        a = 1        b = 1         def calcMath(i, a, b):            return math.sqrt(math.pow(a, 2) + math.pow(b, 2))         for i in range(TIMES):            c = calcMath(i, a, b)            a += 1            b += 2 
  1. The following screenshot shows the time-to-complete differences between regular Python and PyPy, for both time.py and ...

Get Secret Recipes of the Python Ninja 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.