You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I’d like to suggest a performance improvement in the following line: return np.split(arr, np.where(np.ediff1d(arr) - 1 > 0)[0] + 1)
This can be rewritten more efficiently as:
Although np.ediff1d is designed to compute discrete differences between adjacent elements, it introduces unnecessary overhead by internally creating a new array and performing extra type and shape checks. In contrast, using NumPy slicing with arr[1:] - arr[:-1] achieves the exact same result with lower overhead. This avoids function call dispatch and temporary memory allocation, resulting in improved performance—especially when working with large arrays.
Since this difference array is only used for locating split indices, there’s no benefit from using np.ediff1d over simple slicing. The replacement not only boosts efficiency but also improves code clarity and aligns better with NumPy’s idiomatic practices.
The text was updated successfully, but these errors were encountered:
mdanalysis/package/MDAnalysis/lib/util.py
Line 1862 in e64755c
Hi, I’d like to suggest a performance improvement in the following line:
return np.split(arr, np.where(np.ediff1d(arr) - 1 > 0)[0] + 1)
This can be rewritten more efficiently as:
Although np.ediff1d is designed to compute discrete differences between adjacent elements, it introduces unnecessary overhead by internally creating a new array and performing extra type and shape checks. In contrast, using NumPy slicing with arr[1:] - arr[:-1] achieves the exact same result with lower overhead. This avoids function call dispatch and temporary memory allocation, resulting in improved performance—especially when working with large arrays.
Since this difference array is only used for locating split indices, there’s no benefit from using np.ediff1d over simple slicing. The replacement not only boosts efficiency but also improves code clarity and aligns better with NumPy’s idiomatic practices.
The text was updated successfully, but these errors were encountered: