O'Reilly logo

Python High Performance Programming by Gabriele Lanaro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Optimizing our code

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required