numpy/element-loop
| Rule | element-loop |
|---|---|
| Pack | NumPy |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
A loop that computes one element of an array per pass runs Python once for every value, which was 70 to 530 times slower. Write it as one expression on whole arrays.
out = np.empty_like(a)for i in range(len(a)): out[i] = a[i] * 2 + np.sqrt(b[i])out = a * 2 + np.sqrt(b)What the rule reports
Section titled “What the rule reports”The rule reports a for loop over range, np.arange or np.ndindex, and a while i < n loop with i += 1, when the loop's only statement writes one element of a NumPy array from arithmetic on the loop position. It also reports a loop over np.nditer or np.ndenumerate without external_loop. The rule leaves a loop alone whose body calls another function, reads the array it writes, or writes a slice.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
This loop computes $OUT one element per pass in Python. Write it as one expression on whole arrays, with slices or index arrays in place of $I, which ran 70 to 530 times faster on 1,000,000 values.Browse the other rules of the NumPy pack.