Most of the time, readability is more important than the performance of scientific software. This page contains tips for improving the performance of PlasmaPy for situations where performance becomes a bottleneck.
Upgrade to the newest version of Python to take advantage of ongoing performance improvements from the Faster CPython project. New versions of Python also have improved error message, which can speed up the debugging process too.
A new version of Python is released in October of each year, and can be used with PlasmaPy a few months later.
Using Astropy units
PlasmaPy makes heavy use of
See Astropy’s documentation for performance tips for Quantity
Because PlasmaPy uses SI units internally, performance can be improved
slightly by providing
Quantity objects in SI units to functions in
plasmapy.formulary. Unit conversions done by the
decorator then do not need to be performed.
Many of the functions in
plasmapy.formulary accept particle-like
arguments. Arguments that are not already a
ParticleList are converted into one (usually via
particle_input() decorator. When a formulary function is repeatedly
called, performance can be improved by creating the particle object
ahead of time.
For example, suppose we are calculating the
gyrofrequency of a proton. If we
represent the particle as a string, then the function will need to
Particle each time the function is called.
from plasmapy.formulary import gyrofrequency import astropy.units as u for i in range(1000): gyrofrequency(B=0.02 * u.T, particle="p+") # create the Particle repeatedly
If we create the
Quantity ahead of time, they will need
to be create only once instead of repeatedly.
from plasmapy.particles import Particle B = 0.02 * u.T proton = Particle("p+") # create the Particle once for i in range(1000): gyrofrequency(B=B, particle=proton)
PlasmaPy includes lite-functions for some
functions for situations when performance matters. For example,
plasma_frequency_lite is the
Lite-functions accept and return NumPy arrays (assumed to be
in SI units) instead of
Quantity objects. Lite-functions make use of
just-in-time (JIT) compilation via Numba to achieve high performance.
Because lite-functions do not include any validation of inputs, they
should only be used for performance-critical applications.
If you need a lite-function version of a
that has not already been implemented, please raise an issue.