Why is this loop runs gradually slowly？
Question: The flowing code is a simple python loop.

    
    
    def getBestWeightsByRandomGradientAscent(featureDatasList, classTypes, maxCycles=1):
        """
        :param featureDatasList:
        :param classTypes:
        :param maxCycles: the loop time
        :return:
        """
        import random
    
        featureDatas = np.array(featureDatasList)
        m, n = np.shape(featureDatas)
        weights = np.ones(n)
    
        # the loop goes here... #
        for j in range(maxCycles):
            featureIndexs = range(m)
            featureLen = len(featureIndexs)
            for i in range(m):  
                delta = 4 / (1.0 + i + j) + 0.01
                randIndex = int(random.uniform(0, featureLen))
                sigmodInput = sum(featureDatas[randIndex] * weights)
                estimateClass = calculateSigmodEstimateClassType(sigmodInput)
                error = classTypes[randIndex] - estimateClass
                weights += (error * delta) * featureDatas[randIndex]
                del (featureIndexs[randIndex])
    
        return weights
    

I find that when I run this loop for 1000 or more times,it runs quickly at
beginning,but it goes slower and slower as it runs,and finally keeps a slow
speed...Curious,I don't why.Does it cause by the range of variables or my
hardware problem? How can I fix this problem? Thanks a lot!


Answer: Consider using `xrange` instead of `range`. It may be memory or the GC. You
can profile your code with something like
<http://www.vrplumber.com/programming/runsnakerun/>

