From 7ae0338510cf6bdfb3fceb5cfdf21f38012abc4a Mon Sep 17 00:00:00 2001 From: pfackeldey Date: Wed, 15 Jan 2025 13:54:48 -0500 Subject: [PATCH 1/2] add a performance tipp to ak.transform doc string --- src/awkward/operations/ak_transform.py | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/awkward/operations/ak_transform.py b/src/awkward/operations/ak_transform.py index 93a4911914..cf1b3e3494 100644 --- a/src/awkward/operations/ak_transform.py +++ b/src/awkward/operations/ak_transform.py @@ -425,6 +425,40 @@ def transform( `"none"` The output arrays will not be given parameters. + + Performance Tipp + ================ + + #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. """ From b52693a0cd55e81b401b5e5cc0b46aaa3b3b9417 Mon Sep 17 00:00:00 2001 From: Peter Fackeldey Date: Wed, 15 Jan 2025 15:27:02 -0500 Subject: [PATCH 2/2] fix typo Co-authored-by: Angus Hollands --- src/awkward/operations/ak_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/awkward/operations/ak_transform.py b/src/awkward/operations/ak_transform.py index cf1b3e3494..390f6343f6 100644 --- a/src/awkward/operations/ak_transform.py +++ b/src/awkward/operations/ak_transform.py @@ -426,7 +426,7 @@ def transform( The output arrays will not be given parameters. - Performance Tipp + Performance Tip ================ #ak.transform will traverse the layout of (potentially multiple) arrays once.