X Tutup
Skip to content

ENH: Increase np.atleast_2d and np.atleast_3d performance#30880

Open
benediktjohannes wants to merge 6 commits intonumpy:mainfrom
benediktjohannes:patch-1
Open

ENH: Increase np.atleast_2d and np.atleast_3d performance#30880
benediktjohannes wants to merge 6 commits intonumpy:mainfrom
benediktjohannes:patch-1

Conversation

@benediktjohannes
Copy link

@benediktjohannes benediktjohannes commented Feb 25, 2026

This pull request is a features #26130 by @eendebakpt (credits to him for the great work 👍 ) and should improve the performance of np.atleast_2d and np.atleast_3d.

I've not tested the changes, but it seems quite obvious to me from looking at the code that these should be (at least in most cases) positive (and in general more positive than negative, but I'm not one hundred percent sure, so please correct me if I'm mistaken, thanks).

Credits to: @eendebakpt
Contributed by Benedikt Johannes

Credits to: @eendebakpt
Contributed by Benedikt Johannes
@ngoldbaum

This comment was marked as outdated.

@benediktjohannes

This comment was marked as outdated.

@benediktjohannes

This comment was marked as outdated.

@ngoldbaum

This comment was marked as outdated.

@benediktjohannes

This comment was marked as outdated.

@benediktjohannes

This comment was marked as outdated.

@ngoldbaum

This comment was marked as outdated.

@benediktjohannes benediktjohannes changed the title ENH: Increase np.atleast_2d and np.atleast_2d performance ENH: Increase np.atleast_2d and np.atleast_3d performance Feb 25, 2026
@benediktjohannes
Copy link
Author

I've got the results for the benchmark! 👍

Script: numpytest.py

import pyperf

runner = pyperf.Runner()

setup="""
import numpy as np
from numpy.polynomial import Polynomial

x = np.array([2, 3, 4])
y = np.array([2, 3])

p = Polynomial([1, 2, 3.])
"""   
runner.timeit(name=f"atleast_2d(1)", stmt=f"np.atleast_2d(1)", setup=setup)
runner.timeit(name=f"atleast_2d(x)", stmt=f"np.atleast_2d(x)", setup=setup)
runner.timeit(name=f"atleast_2d(1, x )", stmt=f"np.atleast_2d(1, x )", setup=setup)

runner.timeit(name=f"atleast_3d(1)", stmt=f"np.atleast_3d(1)", setup=setup)
runner.timeit(name=f"atleast_3d(x)", stmt=f"np.atleast_3d(x)", setup=setup)
runner.timeit(name=f"atleast_3d(1, x )", stmt=f"np.atleast_3d(1, x )", setup=setup)

runner.timeit(name=f"polyadd", stmt=f"np.polyadd(x, y)", setup=setup)

(Credits to @eendebakpt for the great script👍)

Results:

Old (numpy original):

atleast_2d(1): Mean +- std dev: 728 ns +- 21 ns
atleast_2d(x): Mean +- std dev: 575 ns +- 25 ns
atleast_2d(1, x ): Mean +- std dev: 1.16 us +- 0.07 us

atleast_3d(1): Mean +- std dev: 761 ns +- 42 ns
atleast_3d(x): Mean +- std dev: 635 ns +- 63 ns
atleast_3d(1, x ): Mean +- std dev: 1.17 us +- 0.04 us

polyadd: Mean +- std dev: 2.88 us +- 0.13 us

New (numpy by Benedikt Johannes):

atleast_2d(1): Mean +- std dev: 662 ns +- 43 ns
atleast_2d(x): Mean +- std dev: 488 ns +- 22 ns
atleast_2d(1, x ): Mean +- std dev: 1.12 us +- 0.05 us

atleast_3d(1): Mean +- std dev: 671 ns +- 22 ns
atleast_3d(x): Mean +- std dev: 512 ns +- 24 ns
atleast_3d(1, x ): Mean +- std dev: 1.15 us +- 0.06 us

polyadd: Mean +- std dev: 2.80 us +- 0.08 us

@ngoldbaum ngoldbaum added this to the 2.5.0 Release milestone Feb 26, 2026
@benediktjohannes
Copy link
Author

Test failure is unrealted (can't install build environment)

@benediktjohannes
Copy link
Author

Should I write release notes 2.5 for this change, @ngoldbaum? Or is this done by the team? I only know this from CPython.

@benediktjohannes
Copy link
Author

Because it says:

Beyond changes to a functions docstring and possible description in the general documentation, if your change introduces any user-facing modifications they may need to be mentioned in the release notes. To add your change to the release notes, you need to create a short file with a summary and place it in doc/release/upcoming_changes. The file doc/release/upcoming_changes/README.rst details the format and filename conventions.

And the change is kind of user facing in my opinion because the performance is changed and users might notice this (and the latest news document also includes performance improvements).

@ngoldbaum
Copy link
Member

minor performance improvements like this don't need a release note

@benediktjohannes
Copy link
Author

All checks pass, do I need to use any command like /merge?

@benediktjohannes
Copy link
Author

So without the unrelated one

@ngoldbaum
Copy link
Member

Please be patient, I added the milestone so this won't get lost before the next major release this summer.

@benediktjohannes
Copy link
Author

benediktjohannes commented Feb 26, 2026

Thank you!

@benediktjohannes
Copy link
Author

But one question: Is everything finished that I got to do? So can I just wait for the summer without doing anything for this?

@ngoldbaum
Copy link
Member

ngoldbaum commented Feb 26, 2026

Maybe sooner, this PR has only been open for a day or so. The project doesn't move terribly quickly and people do code review as their time and interest permits.

I was also planning to look closer at this at some point. Usually we like to leave PRs that make performance improvements and add new features open for at least a few days.

@ngoldbaum
Copy link
Member

Also, IMO this PR shouldn't be backported, so in practice very few people will be able to use this until NumPy 2.5 comes out this summer.

@benediktjohannes
Copy link
Author

Ah, okay, thank you very much for the Information! Don’t feel stressed about it!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

X Tutup