In Python which is better in terms of performance:
1)
for i in range(len(a[:-1])):
foo()
or
2)
for i in range(len(a)-1):
foo()
UPDATE:
Some context on why I'm looping over indices (non-idiomatic?):
for c in reversed(range(len(self._N)-1)):
D[c] = np.dot(self._W[c], D[c-1])*A[c]*(1-A[c])
The second one is better, two reasons:
The first one created a new list a[:-1], which takes up unnecessary time & memory, the second one didn't create a new list.
The second one is more intuitive and clear.
[:] returns a
shallow copyof a list. it means that every slice notation returns a list which have new address in memory, but its elements would have same addresses that elements of source list have.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With