I have been asked by a user to look at some VBA code that he has in a spreadsheet for him as he keeps getting out of memory error's
Now i have looked at the code and as far as i can see it's use of Massive nested loops is part of the problem.
Unfotunatley i don't fully understand what he is trying to do, i believe that it is a system to predict future prices by taking an initial price and by using a calculation & a random factor produce 3 new numbers on the next level so you build up a tree like structure of prices. This continues on down 100 times until a future price can be predicted, through an averaging of values.
The code in the attached spreadsheet (zipped up) will give you a clearer idea of what he is trying to achieve !
Now can anybody think of a more memory efficient way of doing this ?? as i am a little lost.
Out of memory errors in loops are usually caused by an infinite loop, one that never reaches an end point. How much memory is in the user's machine?
If the loops are basically doing the same thing, instead of having tons of nested loops, maybe recursion would work better. Just make sure it ends at some point.
Recursion is when a function or sub calls itself. An example (I didn't download your code, this is just an example) -
VB Code:
Private Sub RecursiveX(ByRef x as Integer)
If x > 0 Then
If x = 10 Then
x = x * 3.1415
Exit Sub
Else
x = x + 1
RecursiveX(x) 'recursive call
End If
Else
x = 0
End If
End Sub
Private Sub CallRecursiveX()
dim i as Integer
i = 0
RecursiveX(i) 'i = 0 - was set to 0 in RecursiveX
i = 3
RecursiveX(i) 'i = 30.1415 - was incremented to 10 via recursive calls, then multiplied
i = 10
RecursiveX(i) 'i = 30.1415 - no recursive call
i = 20
RecursiveX(i) 'i = ?? - this will be an infinite loop till memory runs out,
' because there's nothing to end the recursive calls
End Sub
That's not an exciting example, and recursive calls can have memory problems too (they keep adding to the stack), but it's easier to read than tons of nested functions.
If there are functions nested more than 5 deep, you've got too many. 3 or 4 are better.
Last edited by salvelinus; Jan 14th, 2005 at 11:20 AM.
Unfotunatley i don't fully understand what he is trying to do, i believe that it is a system to predict future prices by taking an initial price and by using a calculation & a random factor produce 3 new numbers on the next level so you build up a tree like structure of prices. This continues on down 100 times until a future price can be predicted, through an averaging of values.
LOL! OK, this is one case where more is not better. The link below describes what he is trying to do, and if I were you, I'd draw this part to his attention:
Originally Posted by www.riskglossary.com
While increasing the sample size is one technique for reducing the standard error of a Monte Carlo analysis, doing so can be computationally expensive. A better solution is to employ some technique of variance reduction. These techniques incorporate additional information about the analysis directly into the estimator. This allows them to make the Monte Carlo estimator more deterministic, and hence have a lower standard error.
If you really want a crack at coding this, this link will get you started: