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

docs: add a performance tip to ak.transform docstring #3369

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/awkward/operations/ak_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,40 @@ def transform(
`"none"`
The output arrays will not be given parameters.


Performance Tip
================

#ak.transform will traverse the layout of (potentially multiple) arrays once.
This can be useful if one wants to apply a batch of transformations in one single
layout traversal. Traversing the layout multiple times can be inefficient.

Consider the following example:

>>> def batch_of_operations(array):
... return np.sqrt(np.sin(array) + 1) - 1
...
>>> def apply_batch_of_operations(layout, **kwargs):
... if layout.is_numpy:
... return ak.contents.NumpyArray(
... batch_of_operations(layout.data)
... )
...
>>> array = ak.Array(
... [[[[[1.1, 2.2, 3.3], []], None], []],
... [[[[4.4, 5.5]]]]]
... )
>>> %timeit ak.transform(apply_batch_of_operations, array)
... 68.5 μs ± 663 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
>>> %timeit batch_of_operations(array)
... 1.07 ms ± 39.1 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

The first `%timeit` cell shows the time it takes to apply the batch of operations using #ak.transform,
which allows to apply the operations in one single traversal of the layout. The second `%timeit` cell shows
the runtime of applying the operations directly to the array, which traverses the layout multiple times.
To be more explicit: one layout traversal for each operation.


See also: #ak.is_valid and #ak.valid_when to check the validity of transformed
outputs.
"""
Expand Down
Loading