Hi
So, a fast list search method/s is needed.
I try to locate the value’s ‘slot’ in the list. See sample list on below:
a = [-9.0, -5.7, -1.2, 0, 1.7, 5.4, 9.0]
Lets say, the value is 3.5. I can easily see, it’s between 4 an 5 list ‘slots’.
How to do this very fast with python?
Thanks
edit
-------------------------------------------------------------------------------------
You can see on below a speed test code, where I’m comparing a IUS- and i3-functions speed performance.
Both functions are providing a same result from same source data’s.
I try to optimize the i3 function as much as possible. Now it’s about 30 times faster, than IUS function.
Should/can I change the source data structure to be able to create faster function?
Any hints?
BTW
Forget to mention, the IUS-function requires a scipy and numpy installation.
from time import clock as c
from scipy.interpolate import InterpolatedUnivariateSpline
aoa = [-90.0, -45.0, -30.0, -20.0, 0.0, 20.0, 30.0, 45.0, 90.0]
vcl = [0.0, -0.95, -0.2, -1.0, 0.0, 1.0, 0.2, 0.95, 0.0]
def IUS(x, y, v):
return InterpolatedUnivariateSpline(x, y, k=1)(v)
def i3(x, y, v):
for i , val in enumerate(x):
try:
if (v >= val) and (v < aoa[i+1]):
return y[i] + ((v - x[i])*(y[i+1]-y[i])) / (x[i+1]-x[i])
except IndexError:
return y[0]
#### -----------------------------
#### sweep test settings
vl = 90.0 # sweep test range -90 to 90 degrees aoa
av = 0.01 # sweep test step
#### -----------------------------
t0 = c() # start time
v = -vl
while v <= vl:
IUS(aoa, vcl, v)
v +=av
val1 =c() - t0 # cal time
#### -----------------------------
t0 = c() # start time
v = -vl
while v <= vl:
i3(aoa, vcl, v)
v +=av
val2 = c() - t0 # cal time
#### -----------------------------
#### show results
print 'IUS time:',round(val1,3), 'seconds'
print 'i3 time:',round(val2,3), 'seconds'
print round(val1 / val2,1), 'x speed difference'
#### Test with single values
##v = 90.0
##print i3(aoa, vcl, v)
##print IUS(aoa, vcl, v)
Thanks again