Now that we have identified exactly how the time is spent, we can modify the code and assess the change in performance.

There are a few different ways to tune up our pure Python code. The way that usually produces the most remarkable results is to change the *algorithm*. In this case, instead of calculating the velocity and adding small steps, it would be more efficient (and correct, as it is not an approximation) to express the equations of motion in terms of radius `r`

and angle `alpha`

(instead of `x`

and `y`

), and then calculate the points on a circle using the equation:

x = r * cos(alpha) y = r * sin(alpha)

Another way lies in minimizing the number of instructions. For example, we can pre-calculate the factor `timestep * p.ang_speed ...`

Start Free Trial

No credit card required