Some real-world examples
Here are more examples of how PyPy can improve performance, as well as some practical uses of the environment.
How to do it...
- 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
- The following code (
time2.py
) does the same thing aspythag_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
- The following screenshot shows the time-to-complete differences between regular...