-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Description
Line 29 in d595486
sumA = np.sum(A) |
Hi, I’d like to suggest a small performance improvement in this code:
A = np.exp(-D * beta)
sumA = np.sum(A)
This can be more efficiently written as:
A = np.exp(-D * beta)
sumA = A.sum()
Since A is the direct result of a NumPy ufunc (np.exp), it is guaranteed to be a NumPy ndarray. When using np.sum(A), NumPy performs a layer of general-purpose dispatching, including checking the input type, handling duck-typed objects, and possibly resolving array_function overrides. Even though these checks are redundant here, they still introduce minor overhead. In contrast, A.sum() directly invokes the internal C implementation associated with the array object itself, bypassing unnecessary logic and executing with minimal function call depth. This makes it the preferred choice in performance-sensitive contexts where the input type is known.
Metadata
Metadata
Assignees
Labels
No labels