Vectorization a code to make it faster than this


I have a little bit code which I’ll have to vectorizate it to make it faster. I’m not very attached into python and thinking that the for loop is not so efficient.

Is there any way to reduce the time?

import numpy as np
import time

start = time.time()

N = 10000000 #9 seconds
#N = 100000000 #93 seconds

alpha = np.linspace(0.00000000000001, np.pi/2, N)

tmp = 2.47*np.sin(alpha)
for i in range(N):
    if (abs(tmp[i])>1.0):
beta = np.arcsin(tmp)

end = time.time()
print("Executed time: ",round(end-start,1),"Seconds")

I read about some numpy functions but I don’t have a solution for this.

>Solution :

Clip the array:

tmp = np.clip(2.47 * np.sin(alpha), -1.0, 1.0)

Leave a Reply Cancel reply