Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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):
        tmp[i]=1.0*np.sign(tmp[i])
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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Clip the array:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading