I have been banging my head against the wall for the last couple days, trying to optimize my code to improve speed. This chunk of code right here however is still quite slow, and I am not quite sure how to improve its speed specifically.
The problem, when using speed tests, seems to be primarily the nested for loop. The loop that calculates distance seems to be relatively quick.
As an explanation of the code, I am reading in images using openCV, going through that image, and finding the nearest pixel to a given location. I want to ignore certain colors within my conditional in the nested for loop, as well as ignore previously visited pixels elsewhere in my code.
found = False
randArray = []
for i in range(0,imgCol):
for j in range(0,imgRow):
if(pixelInLine[j*imgCol + i] == 0 and numpy.any(img2[j, i] != 0) and isWhite(j,i) == False):
found = True
temp = points(j,i)
pointArray.append(temp)
#The pixelsInLine is to ignore previously visited pixels
#The rest of the above conditional is to ignore colors I don't want
myDist = 0
currDist = sys.maxsize
distArray = []
for sx in pointArray:
myDist = math.sqrt( ((currR-sx.rr)**2)+((currC-sx.cc)**2))
if myDist == currDist:
distArray.append(sx)
if myDist < currDist:
distArray = []
distArray.append(sx)
currDist = myDist
if found == True:
rrr = distArray[0].rr
ccc = distArray[0].cc