Info

This question is closed. Reopen it to edit or answer.

Program gets exponentially slower as iterations increase (even though array is preallocated)

1 view (last 30 days)
Hello,
I'm having problems running the program seen below. It is a Monte Carlo Simulation of photon propagation. My graphs appears to be correct when they appear, but the problem is that my program gets slower and slower the longer I run it. At times running N=1000 takes 20 seconds, other times minutes, other times hours.
I need to run this at a 1 million, which is infeasible currently. I expect 1 million to take just a few hours. The problem is likely due to memory allocation from what I've read, but I only have one array that I create in which I am simply updating values. Is there another part of my code that is taking up memory? Thanks!
  3 Comments
Image Analyst
Image Analyst on 15 Nov 2015
Do you have the Parallel Computing Toolbox? I suppose each run could take longer or shorter depending on what kind of events happened during the photon's lifetime. You could put an artificial cap on the number of iterations by using a failsafe variable on the while command ( always a wise thing to do).
dpb
dpb on 15 Nov 2015
Well, I haven't had time to do much but I started your code and let it run for just a little while and looked at
>> whos A
Name Size Bytes Class Attributes
A 201x201 323208 double
>> [irdown irup]
ans =
826 827
>> [izdown, izup]
ans =
969 970
>>
Which are far outside the boundaries -- looks like you need some logic to shortcircuit a bunch of computations to not spend excess time computing what isn't of interest mayhaps...like a break or two judiciously scattered about???

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!