Code runs very slow after making small changes

조회 수: 11 (최근 30일)
hg24
hg24 2024년 2월 26일
댓글: Walter Roberson 2024년 9월 3일
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
Víctor Wilfried Staels
Víctor Wilfried Staels 2024년 9월 3일
이동: Walter Roberson 2024년 9월 3일
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 2024년 9월 3일
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.

댓글을 달려면 로그인하십시오.

답변 (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.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by