slowdown after each loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newmaid
    New Member
    • Feb 2008
    • 5

    slowdown after each loop

    Hi all

    just put together this little bit of code... I am sure it is heavily flawed:(... but can someone please point out to me the reason why it becomes slower after every loop. thanks!!!

    ** ....i am using "globals()[]" to try and create dynamic variables.. i am guessing that this a very bad way to do it?

    Code:
    j = 0
    
    while j < x:
    
        s = 0
        
        while s < len(securityNamesList):
            
            globals()[securityNamesList[s] + 'adjustedHPR'] = []
    
            k = 0
            while k <  len(globals()[securityNamesList[0] + 'dollarHPRS']):
              
                                  
                globals()[securityNamesList[s] + 'adjustedHPR'].append(globals()[securityNamesList[s] + 'FinalDistribution'][j] * globals()[securityNamesList[s] + 'dollarHPRS'][k])
                k = k + 1
           
            s = s + 1
    
        k = 0
        while k < len(globals()[securityNamesList[0] + 'adjustedHPR']):    
            s = 0
            todaysHPR = 0
            while s < len(securityNamesList):
            
                todaysHPR = todaysHPR + globals()[securityNamesList[s] + 'adjustedHPR'][k] 
                
                s = s + 1
    
            portfolioHPR.append( 1 + todaysHPR )
            k = k + 1
    
    
        portfolioDailyMeanReturn = stats.lmean(portfolioHPR)
        portfolioDailyStandardDeviation = stats.stdev(portfolioHPR)
        portfolioAnnualStandardDeviation = portfolioDailyStandardDeviation * 16
        portfolioDailyEstimatedGeometricMean = math.sqrt(portfolioDailyMeanReturn ** 2 - portfolioDailyStandardDeviation ** 2 )
        portfolioAnnualEstimatedGemoetricMean = portfolioDailyEstimatedGeometricMean ** 256
        portfolioSharpeRatio = (portfolioAnnualEstimatedGemoetricMean - 1) / portfolioAnnualStandardDeviation
        sharpeRatioList.append(portfolioSharpeRatio)
        
    
        
    
        j = j + 1
    thanks again!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by newmaid
    Hi all

    just put together this little bit of code... I am sure it is heavily flawed:(... but can someone please point out to me the reason why it becomes slower after every loop. thanks!!!

    ** ....i am using "globals()[]" to try and create dynamic variables.. i am guessing that this a very bad way to do it?

    Code:
    j = 0
    
    while j < x:
    
        s = 0
        
        while s < len(securityNamesList):
            
            globals()[securityNamesList[s] + 'adjustedHPR'] = []
    
            k = 0
            while k <  len(globals()[securityNamesList[0] + 'dollarHPRS']):
              
                                  
                globals()[securityNamesList[s] + 'adjustedHPR'].append(globals()[securityNamesList[s] + 'FinalDistribution'][j] * globals()[securityNamesList[s] + 'dollarHPRS'][k])
                k = k + 1
           
            s = s + 1
    
        k = 0
        while k < len(globals()[securityNamesList[0] + 'adjustedHPR']):    
            s = 0
            todaysHPR = 0
            while s < len(securityNamesList):
            
                todaysHPR = todaysHPR + globals()[securityNamesList[s] + 'adjustedHPR'][k] 
                
                s = s + 1
    
            portfolioHPR.append( 1 + todaysHPR )
            k = k + 1
    
    
        portfolioDailyMeanReturn = stats.lmean(portfolioHPR)
        portfolioDailyStandardDeviation = stats.stdev(portfolioHPR)
        portfolioAnnualStandardDeviation = portfolioDailyStandardDeviation * 16
        portfolioDailyEstimatedGeometricMean = math.sqrt(portfolioDailyMeanReturn ** 2 - portfolioDailyStandardDeviation ** 2 )
        portfolioAnnualEstimatedGemoetricMean = portfolioDailyEstimatedGeometricMean ** 256
        portfolioSharpeRatio = (portfolioAnnualEstimatedGemoetricMean - 1) / portfolioAnnualStandardDeviation
        sharpeRatioList.append(portfolioSharpeRatio)
        
    
        
    
        j = j + 1
    thanks again!
    It's hard for me to follow without seeing some of the data objects you are processing. Try creating a separate dictionary dd instead of manipulating the global dictionary. When you are finished, you can update the global dictionary: globals().updat e(dd)

    I prefer to use the augmented assignment operator.[code=Python]k +=1
    # instead of
    k = k+1[/code]It won't affect the speed though. You may be able to calculate todaysHPR like this:[code=Python]todaysHPR = sum([globals()[name + 'adjustedHPR'][k] for name in securityNamesLi st])[/code]

    Comment

    • newmaid
      New Member
      • Feb 2008
      • 5

      #3
      Originally posted by bvdet
      It's hard for me to follow without seeing some of the data objects you are processing. Try creating a separate dictionary dd instead of manipulating the global dictionary. When you are finished, you can update the global dictionary: globals().updat e(dd)

      I prefer to use the augmented assignment operator.[code=Python]k +=1
      # instead of
      k = k+1[/code]It won't affect the speed though. You may be able to calculate todaysHPR like this:[code=Python]todaysHPR = sum([globals()[name + 'adjustedHPR'][k] for name in securityNamesLi st])[/code]
      thanks for your response bvdet. I think i found the problem. the portfolioHPR list was not being emptied after each loop .. and so was just getting bigger and bigger.

      i have added [code=python] del porftolioHPR[:] [/code] after the calcs and it seems to have stopped the problem.

      in the meantime I will try all your tips

      thanks again

      Comment

      Working...