Code runs very slow after making small changes

8 Ansichten (letzte 30 Tage)
hg24
hg24 am 26 Feb. 2024
Kommentiert: DGM am 7 Mär. 2024
I have a script containing ~1500 lines that I run in sections as each section produces a different plot. If I run from scratch after clearing everything, each section runs as expected, taking 20-30 seconds each. However, if I make any change (e.g. something as small as changing the colours in a plot), it then gets stuck. Rather than taking 20-30 seconds, a section could take over 2 minutes. I realised this was occuring specifically after making changes, so I started running single lines of code to see what was happening. I would enter something simple like x=1; into the command window and this would take a minute or two to complete (which obviously is ridiculous). However, once I had done this, it was as if it had reset, and everything then started running normally again - until I made another change.
I've tried shutting Matlab down and restarting the computer, but the problem persists. It has been like this for about a month now, I've got into a habbit of typing x=1; into the command window after every change I make so I know whether Matlab is ready for me to run the actual script again. Sometimes it isn't too bad, and will only take about 10 seconds, but other times I can be waiting for 5 minutes.
For extra context, this only happens when I am working on scripts that use a fair bit of memory. The one I mention in this example reads in ~500mb of data and also does MCMC analysis with ~200,000 samples.
  6 Kommentare
Rik
Rik am 7 Mär. 2024
Neither I nor John has experienced this ourselves in our years of writing Matlab code. While you may think it unlikely, both John and I expect it to be the source of the issue. It may or may not be. Without the code itself we can't tell for sure.
The only thing we can therefore tell you is how we try to avoid this issue: by using real functions for real work. Scripts are nice for debugging, but not work. I use scripts to figure out what works, but I use functions to make sure I will have the same result tomorrow.
Having a function doesn't mean you can't change anything anymore. You can pass in all the options you like. But clearly something about that 1500 lines of code causes Matlab problems. I have a 10k lines files that runs without problems, even with GNU Octave (a slow, semi-alternative to Matlab) on a Raspberry Pi. But those 10k lines are divided into 179 function, some off which have several dozen lines of documentation.
DGM
DGM am 7 Mär. 2024
There's plenty of ways you can burn a ton of time with an unexpectedly expensive plot creation or manipulation within a loop. We still can't do anything but guess. I don't even know if a loop is involved.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Constantino Carlos Reyes-Aldasoro
Are you plotting your results? If you are, and you are doing many of these, it may be the case that that is what is slowing down your code if every time you are using plot(...). I once had problem like that and solved it by plotting once, grabbing the handle of the plot and then changing the XData and YData of the handles. Hope this helps.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by