Code runs very slow after making small changes

12 views (last 30 days)
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.
  8 Comments
Víctor Wilfried
Víctor Wilfried on 3 Sep 2024
Moved: Walter Roberson on 3 Sep 2024
Hi @hg24 Did you manage to solve your problem? I have the same one. Even writing down in the command window "2+2" will cause a huge execution time after changing something in the script. I wonder if this is related to some backgroud checking MATLAB is doing when the script changes, like the license check or something like that.
Walter Roberson
Walter Roberson on 3 Sep 2024
One of the causes of MATLAB taking a long time between trivial statements, is if the search path contains a network drive, and the network drive is slow or hung.

Sign in to comment.

Answers (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.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!