Repeatedly entering and exiting debug mode speeds up script execution

I asked a similar question to this one a while ago on a different account (https://it.mathworks.com/matlabcentral/answers/548595-execution-times-and-the-pause-function) but did not manage to resolve what seems to be the underlying issue, so I am trying again with some new information:
I am running a script in Matlab which essentially looks like this:
N=30000;
T=876;
times=zeros(1,T);
for 1=1:T
disp(t)
%%%(A BUNCH OF OTHER OPERATIONS)%%%
tic()
for 1=1:N
%stuff
end
times(t)=toc()
end
i.e. I run through the 1:N loop 876 times. Each time I record the execution time of that second loop using tic() toc(). The operations performed inside the 1:N loop are always the same, just the numerical values of variables operated on may differ between different t. disp(t) is used so I can monitor the progress of the script in the console.
In the beginning, the execution of the script is very fast (i.e. t goes up fast) but then it suddenly slows down for no apparent reason (see first picture which shows a time-series of the variable "times").
I am aware that there is a whole host of potential reasons for this, but the phenomenon which puzzles me is the following: If I click "Pause" to enter debug mode as soon as I see t going up more slowly and then immediately click "Continue" (exiting debug mode), the execution speeds back up for a while, until the same occurs and I click "Pause" and "Continue" again (see second picture).
As can be gauged from the two figures, the differences in total execution times are quite drastic. Both runs shown above are completely identical in terms of the operations performed, numerical values, pseudo-random numbers etc. etc. The only difference is whether or not I click "Pause" and "Continue" throughout the run. My question is hence: What exactly happens when I tell Matlab to enter debug mode? Is there something it does which might be the explanation for this?
If so, is there a way to replicate this without having to manually enter debug mode all the time?
A few additional bits of information:
  • Based on monitoring task manager, it does not appear to be a memory issue. Memory usage does not change between the two runs.
  • One thing I am noticing when monitoring task manager is that CPU usage spikes when clicking "Continue", then returns to a normal level, and then appears to drop off somewhat when the execution slows back down.
  • This behaviour DOES NOT occur if I simply insert e.g. pause(0.001) under some condition (e.g. pause(0.001) if time(t)>0.1 or something like this). It only occurs when I click the pause button such that debug mode is entered.
  • Vectorisation is not an option to navigate around the issue. What I want to do cannot be done without the 1:N for-loop.
  • The behaviour does not appear to be related to JIT as it also occurs under feature('jit','off') and feature('accel', 'off')
UPDATE:
Today I had the opportunity to try running the same script on a different computer with Matlab R2017b. Same phenomenon, hence it also does not appear to be some kind of hardware issue.

5 Comments

I think, Matlab has a higher priority, when running in debug-mode. When not in debug, "Matlab is just another background process". After exiting debug mode, it's still detected as a process with high priority, but after a while returning back to a normal priority.
This were just some thoughts I had and a simple explanation, but could be caused by anything else
Many thanks for your answer! I tried setting the priority of Matlab to "high" in Task Manager but unfortunately it doesn't appear to make a difference...
What a stange issue. Make sure you have set the priority with an administrative account. Also as this seems very stange what hardware are you running? (CPU, GPU, SSD, RAM).
If you have checked everything else and the RAM use is constant I would wonder if MATLAB uses a paging file system which might explain the behavour. I suspect a member of MATLAB staff may have a better idea of the internal workings of the processes. Maybe try the new @-mention for a staff member if it's still an issue!
Thanks for your answer! The priority is indeed being set from an admin account.
My hardware is as follows:
CPU: i7-7820HQ
GPU: NVIDIA Quadro M2200
SSD: Samsung MZVLB512HAJQ, 500GB
RAM: 32 GB DDR4 (2400 MHz)
I have however had the opportunity to run the same script on two other machines with different configurations and encountered the same problem so I believe it is unlikely to be an issue related to my hardware specifically.
Thank you for the suggestion. @Cris LaPierre @Alan Weiss @Edric Ellis @Abhishek Gupta can any of you help or suggest a staff member who might have an idea what is going on here? I have been dealing with this issue for months now and so far no clue whatsoever how to solve it.
You are using laptop to run your code. I'd suspect thermal throttling, especially since you pause the script, that allows your CPU to cool off a bit and, when you resume, CPU usage goes high, temperature goes high, and then thermal throttle happens. There is a software that tracks this stuff with the status of thermal throttling, but I can't recall of name.
  • Track your CPU frequencies, is it in Turbo Boost all the time? If yes, are all cores on max frequencies? (Now I see the second bullet)
  • Run profiler, check where your code takes long to execute
  • The other computer was laptop or desktop?

Sign in to comment.

Categories

Products

Release

R2019b

Asked:

on 26 Jan 2021

Answered:

on 18 Feb 2021

Community Treasure Hunt

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

Start Hunting!