I have a method in Python that is extremely slow:
def patternRecognition(self):
patRecStartTime = time.time()
plotPatAr = []
patFound = 0
for eachPattern in self.patternAr:
sim1 = 100.00 - abs(self.percentChange(eachPattern[0], self.patForRec[0]))
if sim1 > 50:
sim2 = 100.00 - abs(self.percentChange(eachPattern[1], self.patForRec[1]))
if sim2 > 50:
sim3 = 100.00 - abs(self.percentChange(eachPattern[2], self.patForRec[2]))
if sim3 > 50 :
sim4 = 100.00 - abs(self.percentChange(eachPattern[3], self.patForRec[3]))
if sim4 > 50 :
sim5 = 100.00 - abs(self.percentChange(eachPattern[4], self.patForRec[4]))
if sim5 > 50:
sim6 = 100.00 - abs(self.percentChange(eachPattern[5], self.patForRec[5]))
if sim6 > 50 :
sim7 = 100.00 - abs(self.percentChange(eachPattern[6], self.patForRec[6]))
if sim7 > 50 :
sim8 = 100.00 - abs(self.percentChange(eachPattern[7], self.patForRec[7]))
if sim8 > 50:
sim9 = 100.00 - abs(self.percentChange(eachPattern[8], self.patForRec[8]))
if sim9>50:
sim10 = 100.00 - abs(self.percentChange(eachPattern[9], self.patForRec[9]))
howSim = (sim1+sim2+sim3+sim4+sim5+sim6+sim7+sim8+sim9+sim10)/10.00
if howSim > 70:
patFound = 1
plotPatAr.append(eachPattern)
if patFound == 1:
for eachPatt in plotPatAr:
futurePoints = self.patternAr.index(eachPatt)
if self.performanceAr[futurePoints] > self.patForRec[29]:
#do something
else:
#do something
Does anyone have any suggestions on making this method execute quicker? When the array gets to 50,000 elements, it takes about 1 second to execute and increases as the array patternAr
grows. I read that converting the for
loop into a map would make it quicker, however, I have not been able to figure it out.
I should mention that percentChange
is very simple:
def percentChange(self, startPoint,currentPoint):
try:
x = ((float(currentPoint)-startPoint)/abs(startPoint))*100.00
if x == 0.0:
return 0.000000001
else:
return x
except:
return 0.0001
Initial code with sample data here.
for eachPattern ...
loop and how much in theif patFound == 1
part? How many patterns are placed in theplotPatAr
list? \$\endgroup\$self.performanceAt
? It appears to be a dictionary where the keys are lists. \$\endgroup\$