-
Notifications
You must be signed in to change notification settings - Fork 13
Open
Description
| arr = np.array(array_list, dtype=arr.flat[0].dtype) if isinstance(arr.flat[0], np.ndarray) else array_list |
Hi, I’d like to suggest a small performance optimization to the following expression:
arr = np.array(array_list, dtype=arr.flat[0].dtype) if isinstance(arr.flat[0], np.ndarray) else array_listThis can be simplified and made more efficient as:
arr = np.array(array_list, dtype=arr.item(0).dtype) if isinstance(arr.item(0), np.ndarray) else array_list The use of arr.flat[0] introduces unnecessary overhead by first constructing a flatiter object and then performing index resolution through the iterator interface. In contrast, arr.item(0) is a direct call to a C-level function that extracts the scalar value at the specified flat index without intermediate object creation. It is faster, semantically cleaner, and returns a Python-native object that behaves more predictably in type checks and scalar operations. Since the goal here is simply to detect the type and extract the .dtype attribute from the first element, item(0) is the more appropriate and performant choice.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels