473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. j = 0
  2.  
  3. while j < x:
  4.  
  5.     s = 0
  6.  
  7.     while s < len(securityNamesList):
  8.  
  9.         globals()[securityNamesList[s] + 'adjustedHPR'] = []
  10.  
  11.         k = 0
  12.         while k <  len(globals()[securityNamesList[0] + 'dollarHPRS']):
  13.  
  14.  
  15.             globals()[securityNamesList[s] + 'adjustedHPR'].append(globals()[securityNamesList[s] + 'FinalDistribution'][j] * globals()[securityNamesList[s] + 'dollarHPRS'][k])
  16.             k = k + 1
  17.  
  18.         s = s + 1
  19.  
  20.     k = 0
  21.     while k < len(globals()[securityNamesList[0] + 'adjustedHPR']):    
  22.         s = 0
  23.         todaysHPR = 0
  24.         while s < len(securityNamesList):
  25.  
  26.             todaysHPR = todaysHPR + globals()[securityNamesList[s] + 'adjustedHPR'][k] 
  27.  
  28.             s = s + 1
  29.  
  30.         portfolioHPR.append( 1 + todaysHPR )
  31.         k = k + 1
  32.  
  33.  
  34.     portfolioDailyMeanReturn = stats.lmean(portfolioHPR)
  35.     portfolioDailyStandardDeviation = stats.stdev(portfolioHPR)
  36.     portfolioAnnualStandardDeviation = portfolioDailyStandardDeviation * 16
  37.     portfolioDailyEstimatedGeometricMean = math.sqrt(portfolioDailyMeanReturn ** 2 - portfolioDailyStandardDeviation ** 2 )
  38.     portfolioAnnualEstimatedGemoetricMean = portfolioDailyEstimatedGeometricMean ** 256
  39.     portfolioSharpeRatio = (portfolioAnnualEstimatedGemoetricMean - 1) / portfolioAnnualStandardDeviation
  40.     sharpeRatioList.append(portfolioSharpeRatio)
  41.  
  42.  
  43.  
  44.  
  45.     j = j + 1
thanks again!
Feb 25 '08 #1
2 1263
bvdet
2,851 Expert Mod 2GB
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?

Expand|Select|Wrap|Line Numbers
  1. j = 0
  2.  
  3. while j < x:
  4.  
  5.     s = 0
  6.  
  7.     while s < len(securityNamesList):
  8.  
  9.         globals()[securityNamesList[s] + 'adjustedHPR'] = []
  10.  
  11.         k = 0
  12.         while k <  len(globals()[securityNamesList[0] + 'dollarHPRS']):
  13.  
  14.  
  15.             globals()[securityNamesList[s] + 'adjustedHPR'].append(globals()[securityNamesList[s] + 'FinalDistribution'][j] * globals()[securityNamesList[s] + 'dollarHPRS'][k])
  16.             k = k + 1
  17.  
  18.         s = s + 1
  19.  
  20.     k = 0
  21.     while k < len(globals()[securityNamesList[0] + 'adjustedHPR']):    
  22.         s = 0
  23.         todaysHPR = 0
  24.         while s < len(securityNamesList):
  25.  
  26.             todaysHPR = todaysHPR + globals()[securityNamesList[s] + 'adjustedHPR'][k] 
  27.  
  28.             s = s + 1
  29.  
  30.         portfolioHPR.append( 1 + todaysHPR )
  31.         k = k + 1
  32.  
  33.  
  34.     portfolioDailyMeanReturn = stats.lmean(portfolioHPR)
  35.     portfolioDailyStandardDeviation = stats.stdev(portfolioHPR)
  36.     portfolioAnnualStandardDeviation = portfolioDailyStandardDeviation * 16
  37.     portfolioDailyEstimatedGeometricMean = math.sqrt(portfolioDailyMeanReturn ** 2 - portfolioDailyStandardDeviation ** 2 )
  38.     portfolioAnnualEstimatedGemoetricMean = portfolioDailyEstimatedGeometricMean ** 256
  39.     portfolioSharpeRatio = (portfolioAnnualEstimatedGemoetricMean - 1) / portfolioAnnualStandardDeviation
  40.     sharpeRatioList.append(portfolioSharpeRatio)
  41.  
  42.  
  43.  
  44.  
  45.     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().update(dd)

I prefer to use the augmented assignment operator.
Expand|Select|Wrap|Line Numbers
  1. k +=1
  2. # instead of
  3. k = k+1
It won't affect the speed though. You may be able to calculate todaysHPR like this:
Expand|Select|Wrap|Line Numbers
  1. todaysHPR = sum([globals()[name + 'adjustedHPR'][k] for name in securityNamesList])
Feb 25 '08 #2
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().update(dd)

I prefer to use the augmented assignment operator.
Expand|Select|Wrap|Line Numbers
  1. k +=1
  2. # instead of
  3. k = k+1
It won't affect the speed though. You may be able to calculate todaysHPR like this:
Expand|Select|Wrap|Line Numbers
  1. todaysHPR = sum([globals()[name + 'adjustedHPR'][k] for name in securityNamesList])
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
Expand|Select|Wrap|Line Numbers
  1.  del porftolioHPR[:] 
after the calcs and it seems to have stopped the problem.

in the meantime I will try all your tips

thanks again
Feb 26 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Trevor Perrin | last post by:
After running a simple loop around 100-200 million times, there's a speed drop of about a factor of 7. This happens within several minutes (on a 1.7 Ghz machine), so it's not that hard to see. ...
4
by: Hallvard B Furuseth | last post by:
I have a program which starts by reading a lot of data into various dicts. When I moved a function to create one such dict from near the beginning of the program to a later time, that function...
3
by: Nicke | last post by:
Hi, I've used vb.net for 3 hours(!) now and have probably a very easy question. In my VB6 programs I use For Each Cell in Range... very often which not seems to work in vb.net. Ex. Dim Cell...
8
by: Sebastian Werner | last post by:
Howdy, I currently develop the javascript toolkit qooxdoo (http://qooxdoo.sourceforge.net), some of you heard it already. We have discovered a slowdown on Internet Explorers performance when...
0
by: vojtech | last post by:
Hi, I would like to remark on a problem described by Stephen Livesey almost 3 years ago, about the slowdown he had experienced with an upload of several millions of rows. ...
22
by: Bradley | last post by:
Has anyone else noticed this problem? I converted the back-end to A2000 and the performance problem was fixed. We supply a 97 and 2000 version of our software so we kept the backend in A97 to make...
17
by: andreas.eisele | last post by:
I should have been more specific about possible fixes. In a related thread on http://bugs.python.org/issue2607 Amaury Forgeot d'Arc suggested a setting of the GC thresholds that actually...
0
by: guillaume weymeskirch | last post by:
Hello everybody, To test the python 2.5 garbage collector, I wrote a trivial script allocating dummy objects of various sizes, then forgetting them in a loop. The garbage collector seems...
1
by: Terry Reedy | last post by:
guillaume weymeskirch wrote: On a related note, there have been past threads reporting that allocating and freeing increasingly long arrays, especially of tuples (as I remember) can take more...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.