Skip to content

Avoid needless local variables in Python wrappers#258

Merged
avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:no-needless-local-vars
May 28, 2026
Merged

Avoid needless local variables in Python wrappers#258
avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:no-needless-local-vars

Conversation

@eerovaher

Copy link
Copy Markdown
Contributor

The Python wrappers that can simply return the corresponding ufunc's return value no longer store the ufunc's output in local variables, which makes them slightly faster. For example, on current main

$ ipython -i -c 'from erfa import epb2jd, zp'
Python 3.11.12 (main, May 17 2025, 13:48:36) [Clang 20.1.4 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 9.13.0 -- An enhanced Interactive Python. Type '?' for help.
Tip: Put a ';' at the end of a line to suppress the printing of output.

In [1]: %timeit epb2jd(1957.3)
829 ns ± 1.19 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [2]: %timeit zp()
421 ns ± 0.61 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

but with this PR

In [1]: %timeit epb2jd(1957.3)
810 ns ± 1.59 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [2]: %timeit zp()
418 ns ± 0.631 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

This does not affect any wrappers that have to check the status code the ufunc returns or that take new views of any of the returned arrays.

The Python wrappers that can simply return the corresponding ufunc's
return value (i.e. there is no status code to check and there are no new
views) no longer store the ufunc's output in local variables, which
makes them slightly faster.
@avalentino avalentino merged commit a38e7d7 into liberfa:main May 28, 2026
28 checks passed
@eerovaher eerovaher deleted the no-needless-local-vars branch May 28, 2026 15:54
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.

2 participants