Hackerrank_Python_Domain_Solutions icon indicating copy to clipboard operation
Hackerrank_Python_Domain_Solutions copied to clipboard

[Suggestion] Use np.add.reduce instead of np.sum for slightly better performance

Open SaFE-APIOpt opened this issue 6 months ago • 0 comments

https://github.com/arsho/Hackerrank_Python_Domain_Solutions/blob/502e8e2e00f5d1610be1ed03064366dfd15290e4/Numpy/SumandProd.py#L19 I noticed the following line: s = np.sum(arr, axis=0) If the code is performance-sensitive and does not rely on extra parameters like dtype, keepdims, or initial, you might consider replacing it with: s = np.add.reduce(arr, axis=0) np.sum is a high-level convenience function that internally calls np.add.reduce, but includes additional checks and wrappers. When you're performing simple summation along an axis, using np.add.reduce directly eliminates that overhead and can offer a slight speedup—especially in tight loops or large-scale computations.

SaFE-APIOpt avatar May 08 '25 06:05 SaFE-APIOpt