Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

Numpy array vs python list with time measurement

I have read about numpy array runs faster than ordinary code performed in python. I have a large data to send a function with return value. I test it with numpy index and boolean and with python, I should expect to run the task faster with numpy function but in my test is not the case. Why does it run slower?

from time import time
import numpy as np

def stress(STEEL_E_MODULE, strain_in_rebars, STEEL_STRENGTH):
    STRESS =  STEEL_E_MODULE * strain_in_rebars
    if STRESS <= - STEEL_STRENGTH:
      stress = - STEEL_STRENGTH
    elif STRESS >= - STEEL_STRENGTH and STRESS < 0:
      stress = STRESS
    else:
      stress = min(STRESS, STEEL_STRENGTH)
    return stress

def stressnumpy(STEEL_E_MODULE, strain_in_rebars, STEEL_STRENGTH):
    STRESS = np.array([ STEEL_E_MODULE * strain_in_rebars,  STEEL_E_MODULE * strain_in_rebars])
    Result = np.array([ - STEEL_STRENGTH,  STRESS[1], min(STRESS[1], STEEL_STRENGTH)])
    cond1 = STRESS[0] <= - STEEL_STRENGTH
    cond2 = (STRESS[1] >= - STEEL_STRENGTH) & (STRESS[1] < 0)
    arr = [cond1, cond2, True if cond1 == False and cond2== False else False]
    return Result[arr][0]

tg1 = time()
for n in np.arange(-1000,1000,0.01):
   y = stressnumpy( 2, n, 20)
tg2 = time()
print("time with numpyfunc:", tg2-tg1)


tg3 = time()
for n in np.arange(-1000,1000,0.01):
   x = stress( 2, n, 20)
tg4 = time()
print("time with func:", tg4-tg3)

Time with both functions:

time with numpyfunc: 1.2981691360473633
time with func: 0.1738882064819336

I expect to get opposite running time?

question from:https://stackoverflow.com/questions/65647188/numpy-array-vs-python-list-with-time-measurement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's misguiding to say that a code with arrays runs faster. In general, when people speak about code going faster with Numpy arrays, they point out the benefit of vectorizing the code which can then run faster with Numpy performant implementation of functions for array operations and manipulation. I wrote a code that compares the second code you provided (I slightly modified it to save values on a list in order to compare the results) and I compared it to a vectorized version. You can see that this leads to a significant increase in time and the results given by the two methods are equal:

from time import time
import numpy as np

# We use a list and apply the operation for all elemnt in it => return a list
def stresslist(STEEL_E_MODULE, strain_in_rebars_list, STEEL_STRENGTH):
  stress_list = []
  for strain_in_rebars in strain_in_rebars_list:
    STRESS =  STEEL_E_MODULE * strain_in_rebars
    if STRESS <= - STEEL_STRENGTH:
      stress = - STEEL_STRENGTH
    elif STRESS >= - STEEL_STRENGTH and STRESS < 0:
      stress = STRESS
    else:
      stress = min(STRESS, STEEL_STRENGTH)
    stress_list.append(stress)
  return stress_list

# We use a numpy array and apply the operation for all elemnt in it => return a array
def stressnumpy(STEEL_E_MODULE, strain_in_rebars_array, STEEL_STRENGTH):
  STRESS =  STEEL_E_MODULE * strain_in_rebars_array
  stress_array = np.where(
    STRESS <= -STEEL_STRENGTH, -STEEL_STRENGTH,
    np.where(
      np.logical_and(STRESS >= -STEEL_STRENGTH, STRESS < 0), STRESS,
      np.minimum(STRESS, STEEL_STRENGTH)
    )
  )
  return stress_array

t_start = time()
x = stresslist( 2, list(np.arange(-1000,1000,0.01)), 20)
print(f'time with list: {time()-t_start}s')

t_start = time()
y = stressnumpy( 2, np.arange(-1000,1000,0.01), 20)
print(f'time with numpy: {time()-t_start}s')

print('Results are equal!' if np.allclose(y,np.array(x)) else 'Results differ!')

Output:

% python3 script.py
time with list: 0.24164390563964844s
time with numpy: 0.003011941909790039s
Results are equal!

Please do not hesitate if you have questions about the code. You can also refer to the official documentation for numpy.where, numpy.logical_and and numpy.minimum.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...