Here is an interesting observation:
1 import numpy as np
2 data = np.array([[255,255,255], [0, 0, 255], [255, 0, 0]], np.int8)
3 for i in range(1000000):
4 for row in data:
5 for col in row:
6 flag = col > 0
The above code takes ~17 seconds to finish. If I convert data
to list by doing
data = data.tolist()
Then the whole thing only takes < 1 second to finish.
Would like to know:
1. What's the reason for the low efficiency in ndarray value comparison?
2. What's a more appropriate way to do the comparison if I don't convert the the ndarray to list? Would it be more efficient than if I covert it into list?
Thanks!
-------------- edited question: -------------
As @hpaulj pointed out, it's the iteration not value comparison that's very expensive. But I do need to iterate thru the array. Any better way than converting it to list?