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

Speed up Cube.subset/Coord.intersect #4955

Merged

Conversation

bouweandela
Copy link
Member

@bouweandela bouweandela commented Sep 9, 2022

🚀 Pull Request

Description

This speeds up Cube.subset / Coord.intersect by using a dict lookup instead of list.index to find the indices of the coordinate that should be used for subsetting the cube.

Here is some benchmark code:

import iris.cube
import iris.coords
import numpy as np

lon = iris.coords.DimCoord(np.arange(0, 360, 0.5, dtype=np.float64), standard_name='longitude')
cube = iris.cube.Cube(np.arange(720, dtype=np.float32))
cube.add_dim_coord(lon, 0)
sublon = lon[(lon.points > 60) & (lon.points < 300)]

%timeit cube.subset(sublon)

The current main branch result is:

93.9 ms ± 4.51 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

and the result of running that code with this branch is:

13.5 ms ± 1.54 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

Consult Iris pull request check list

@bouweandela bouweandela marked this pull request as ready for review September 9, 2022 16:01
lib/iris/coords.py Outdated Show resolved Hide resolved
@bouweandela bouweandela force-pushed the faster-cube-subset-coord-intersect branch from 00048fb to c1bc2d6 Compare October 6, 2022 15:18
Copy link
Member

@lbdreyer lbdreyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great, thanks @bouweandela ! Do you mind adding a whatsnew entry to cover this change?

@bouweandela
Copy link
Member Author

bouweandela commented Oct 11, 2022

Thanks for the review @lbdreyer ! I added the entry.

Copy link
Member

@lbdreyer lbdreyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Thanks @bouweandela !

@lbdreyer lbdreyer merged commit 6117f60 into SciTools:main Oct 11, 2022
@bouweandela bouweandela deleted the faster-cube-subset-coord-intersect branch October 11, 2022 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

2 participants