Reputation: 29
Hopefully I can explain this correctly without having to paste a whole wall of code in.
I have a list [0:100]. I need to find the lowest sufficient value in the list that will return a True in another function.
The trick is that I need this to be done fast! so far some of the data has taken over 65 seconds to process, due to the latter function iterating over the specific index in the list over 1000 times.
def search(list,grid):
print(list)
if len(list) <= 2:
return list[0]
else:
p = len(list)//2
print(p)
print(list[p])
if simulation(grid,list[p]) == 'lose':
return search(list[p:len(list)-1],grid)
elif simulation (grid,list[p]) == 'win':
return search(list[0:p],grid)
For a certain input I need the function to return a result in less than 10 seconds. Is there anyway that I can precisely find the value I'm looking for without having to pass every single values from 0 to 100 to the function that relies on the list?
Upvotes: 0
Views: 56
Reputation: 36662
It may not be sufficient, to reach your goal, but one obvious improvement is to avoid recalculating the simulation for the same values, in case it is not a lose.
Removing all print output will speed things up too.
def search(seq, grid):
if len(seq) <= 2:
return seq[0]
p = len(seq)//2
sim_result = simulation(grid, seq[p])
if sim_result == 'lose':
return search(seq[p: len(seq)-1], grid)
elif sim_result == 'win':
return search(seq[0: p], grid)
You will probably have to look at how the simulation is run to improve further.
Upvotes: 2