Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Optimize as_series #25991

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

ENH: Optimize as_series #25991

wants to merge 4 commits into from

Conversation

eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Mar 11, 2024

Optimize numpy.polynomial.polyutils.as_series for the common case (arrays which are not of object dtype).

Benchmark:

import timeit
import numpy as np
from numpy.polynomial.polyutils import as_series
x=np.array([1.])
y=np.array([1.], dtype=object)

number=1_000_000
for alist in [ [x,], [x, x], [x, y]]:
    dt = timeit.timeit('as_series(alist, trim=True)', globals=globals(), number=number)
    print(f'as_series({alist}): {1e6*dt/number:.3f} [us]')

On main:

as_series([array([1.])]): 2.394 [us]
as_series([array([1.]), array([1.])]): 3.820 [us]
as_series([array([1.]), array([1.0], dtype=object)]): 3.349 [us]

This PR:

as_series([array([1.])]): 1.537 [us]
as_series([array([1.]), array([1.])]): 2.736 [us]
as_series([array([1.]), array([1.0], dtype=object)]): 3.190 [us]

@eendebakpt eendebakpt changed the title Draft: ENH: Optimize as_series ENH: Optimize as_series Mar 11, 2024
@mattip
Copy link
Member

mattip commented Apr 4, 2024

What happens for larger array?

@eendebakpt
Copy link
Contributor Author

eendebakpt commented Apr 5, 2024

What happens for larger array?

The main computation time is in overhead and checking the dtypes and shapes, the actual computation with the data is small. Here is a benchmark for larger arrays:

Main:
as_series(['array(float64, shape (1000,))']): 2.241 [us]
as_series(['array(float64, shape (1000,))', 'array(float64, shape (1000,))']): 3.460 [us]
as_series(['array(float64, shape (1000,))', 'array(object, shape (300,))']): 13.872 [us]

PR:
as_series(['array(float64, shape (1000,))']): 1.525 [us]
as_series(['array(float64, shape (1000,))', 'array(float64, shape (1000,))']): 2.598 [us]
as_series(['array(float64, shape (1000,))', 'array(object, shape (300,))']): 14.192 [us]

The as_series is used for coefficients of polynomials. I suspect the arrays will not be much larger than 1000.

@mattip

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.

None yet

2 participants