Skip to content

Changed most calls to pow(a, b) to a**b.#118

Draft
moorepants wants to merge 1 commit into
masterfrom
pow-exponent
Draft

Changed most calls to pow(a, b) to a**b.#118
moorepants wants to merge 1 commit into
masterfrom
pow-exponent

Conversation

@moorepants

Copy link
Copy Markdown
Owner

If you test pow(a, b) versus a**2 you get:

In [1]: %timeit pow(0.5, 2)
195 ns ± 45.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [2]: %timeit 0.5**2
26 ns ± 2.34 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

which is a 7.5 times speed up.

pyinstrument seemed to report that much time was spent in pow() in the eval_linear() function, so I tried swapping (most) of the pow() calls. But with this script:

from bicycleparameters.parameter_dicts import mooreriderlean2012_browser_jason
from bicycleparameters.parameter_sets import MooreRiderLean2012ParameterSet
from bicycleparameters.models import MooreRiderLean2012Model
par_set = MooreRiderLean2012ParameterSet(mooreriderlean2012_browser_jason)
model = MooreRiderLean2012Model(par_set)
vs = np.linspace(0.0, 10.0, num=401)
A, B = model.form_state_space_matrices(v=vs)

I didn't get any speed up with:

(bicycleparameters-dev) moorepants@nandi:BicycleParameters(pow-exponent)$ ipython
Python 3.13.2 | packaged by conda-forge | (main, Feb 17 2025, 14:10:22) [GCC 13.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 9.0.2 -- An enhanced Interactive Python. Type '?' for help.
Tip: You can change the editing mode of IPython to behave more like vi, or emacs.

In [1]: run speed.py

In [2]: %timeit model.form_state_space_matrices()
22.7 ms ± 2.65 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [3]:                                                                                                                                                                                       
Do you really want to exit ([y]/n)? 
(bicycleparameters-dev) moorepants@nandi:BicycleParameters(pow-exponent)$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
(bicycleparameters-dev) moorepants@nandi:BicycleParameters(master)$ ipython
Python 3.13.2 | packaged by conda-forge | (main, Feb 17 2025, 14:10:22) [GCC 13.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 9.0.2 -- An enhanced Interactive Python. Type '?' for help.
Tip: Put a ';' at the end of a line to suppress the printing of output.

In [1]: run speed.py

In [2]: %timeit model.form_state_space_matrices()
21.4 ms ± 1.97 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant