Search code examples
pythonloopsnumpynumpy-ndarraycupy

How to loop numpy ndarray by using cupy? Is will really improve execution time?


I have two numpy ndarray with same size[512 X 512]. I want to change one array according to other array value. But the execution time is really long. So, I want to change numpy ndarray to cupy ndarray and want to execute in GPU. Is it possible? Is will cut the execution time? Here is a piece of my current code.

for n,val in enumerate(array_A.flat):
    if val < 200:
        Array_B.flat[n] = -1000

Solution

  • You will experience a huge boost in performance if you vectorize the option with NumPy's built-in functions:

    Array_B[array_A < 200] = -1000
    

    This pushes the operation to a lower level, avoiding executing the loop in Python.