Random lags while running app
古いコメントを表示
I have an App Designed application that collects data from an Arduino Pico.
The Pico runs individual trials with accurate to submillisecond timing. MATLAB starts each trial as soon as the previous one is complete. Data is saved in tables as I found that writing to disk after each trial took too long. Even so, the time between trials is longer than it should be AND it can be extremely long on occasion. I've plotted each trial duration and the timing of the end of one to the start of the next. Any ideas as to why MATLAB occasionally takes long breaks? I've tried clearing variables between each trial (doesn't seem to affect my app tables). Removed calls to get time of day (useing Pico Timer for data) various other attemps with Profiler but no luck since the error is rare. Copilot wasn't much help either.
I tried updating to version R2025a and that was much worse!

Any help much appreciated. Code for the app is here, it's big:
https://drive.google.com/file/d/1mwCtRJ-l_2fhk26JYzz2IxidDqkU3IV1/view?usp=sharing
16 件のコメント
It's probably not MATLAB itself but the OS behind the scenes. Windows is not a realtime OS; it does task switching and garbage cleanup on its own and who knows what all else is in the background in the startup tasks and processes...including apps calling home to see if any updates are available or the like; Adobe in particularl is terrible about that if you have any of their apps installed if you don't stop them manually from automatically updating in the background.
It is somewhat interesting that there seems to be the beginnings at least of a pattern; there are two peaks roughy the same time interval apart when the lag does occur and in both cases the first is larger than the second. Wonder if that might point to what actually is...
You could use the resource monitor tool and log and perhaps find out what is the culprit that way...perfmon.exe is the underlying tool, resmon.exe is a user interface to it...
Gavin
2025 年 7 月 16 日
dpb
2025 年 7 月 16 日
There are a zillion background processes loaded by the OS besides just what is in the startup folder.
You can raise the priority of a process, but you cannot block other interruptions entirely, no, the OS is going to do what it thinks it needs to do, regardless.
Is this compiled or are you running inside MATLAB?
Gavin
2025 年 7 月 16 日
dpb
2025 年 7 月 16 日
The compiler <mcc<> is another product/TB. I doubt it will solve your problem, I was just trying to find out which way you were/are running. My experience with a compiled app is that it is actually quite a bit slower than the same code running interactively. You'd think otherwise, but it doesn't really compile compile, it builds a call into the MATLAB runtime engine that then executes the code. That, it appears, then doesn't get the benefit of the JIT compiler interactive functions do (hypothesis).
To find your problem, you would have to run a log of the resource monitor at the same time such that you would have a chance to then find the other process that is running at the same time as your delays. May not be an easy thing to find what that is. I wondered about a memory reallocation issue, but given your repetitive nature, I think that would be pretty regular as the same memory amounts would be used repetitively.
Gavin
2025 年 7 月 16 日
Walter Roberson
2025 年 7 月 16 日
That, it appears, then doesn't get the benefit of the JIT compiler interactive functions do (hypothesis).
Code compiled with MATLAB Compiler is pre-parsed and tokenized, and then at run-time, it uses the same execution engine as interactive MATLAB does. It does use the JIT compiler.
Then why does it run so much slower????
I understand the startup to an extent, but my experience has been the same code compiled runs slower than the same code run interactively on the same machine.
ADDENDUM
OK, I just timed it and it is closer than I felt it seemed to be.comparision.
On the home machine, it is actually reasonably close; the comparison data
Run Compiled Interactive Ratio
1 41.65 31.35 1.32
2 32.23 31.96 1.01
3 32.36 31.47 1.03
I'll grant the 2nd and 3rd are the same; not sure why the first compiled case was so much longer? Unfortunately, the way the code is used, it generally is only one run at a time so that advantage is normally lost. I did not try restarting to see if this is reproducible.
Perhaps the ladies' office machines aren't as quick as this one at home so am influenced by an artificial difference; MATLAB isn't installed there so can't do direct comparison on their machines but I know from watching them it takes longer there; the list that is shown in the progress dialog to give indications that something is happening is noticeably slower than the list I just watched go by...but, it does appear that the two are basically the same.
So, if they could do something about the startup time and that black hole that seems interminable after the double-click and one thinks nothing is happening...
dpb
2025 年 7 月 19 日
I just added timing code into an app I built; it is immaterial what the code is for the test to show whether the compiled code and interpreted code run at essentially the same speed. You may be right about other needed DLLs although I only timed calculational code after the UI was loaded to avoid all the startup and initial rendering of the UI; my initial thinking was that would be everything needed with the runtime, but since this code also does COM stuff reading/writing Excel files, there probably are some of those other OS and MS pieces needed that weren't already loaded the first time.
I'm totally confused by the discussion about whether it runs slow or runs fast overall; if there is a total difference in behavior from one invocation to another, that is truly bizarre and I would have no explanation at all unless it has something to do with the Arduino interface communications. I thought from the prior graphs the issue is that it generally runs pretty consistently between acquisitions with then an occasional lag; that type of behavior is consistent with OS context switching and/or other background processes that are scheduled or interrupt driven. A behavior wherein the main MATLAB code has two different timing behaviors is something I can't even think about what would be the cause other than as above since are using the Arduino that there's something going on there, not just in MATLAB itself.
Base MATLAB is single-threaded, that is true although a fair number of the base computation-heavy functions in the matrix library, etc., are built to take advantage of mulitiple cores automatically if the internal logic determines it appears to be a case in which it will improve performance. Your code would have to be using such functions for that to ever be the case.
If your application could run pieces in parallel on the same data, not requiring the results from one part to be completed before the next phase can start, then <the parallell processing toolbox> could possibly help, but with it, the base code does have to be able to be parallelized and you have to code it to take advantage yourself. A white paper on <MATLAB Multicore>.
Gavin
2025 年 7 月 19 日
dpb
2025 年 7 月 19 日
I've no klew, sorry. Is this running in a tight loop or are there timers or other ways to try to schedule the trials?
I can't dream up an explanation of the above behavior unless there is some other process hogging CPU cycles during the "slow" runs that isn't when "fast"
Gavin
2025 年 7 月 20 日
Anything is possible, I suppose, but I keep coming back to that the CPU has to be doing _something_ while the time is being consumed; figuring out what that something is is the problem. If the the task running the MATLAB code is not idle, it has to be doing something, even if that something is a pause...
Gavin
2025 年 7 月 23 日
dpb
2025 年 7 月 23 日
Good spelunking to have uncovered the "where"...can you tell whether it is a delay from the time the MATLAB line is encountered before the transmission or a delay in actually executing the code itself?
I'm not sure there is any way you can control that, at least with MATLAB-level code; you're pretty-much at the mercy of the OS there, I think.
Only a couple of ideas to try otomh; first would be to use the low-level char() array for the data instead of the string class; this is just the string of bytes without the overhead of the string wrapper to be dereferenced on sending.
str='G';
if ~isempty(str)
write(app.PicoCom, str, 'char');
I don't know that if you dropped into mex and C to do the communications you could ensure better performance or not; the internals are all builtin functions already, so it would only be whether that could prevent some sort of delay in the MATLAB code itself between lines of code being dispatched.
What would it take to just build a command line interface that does nothing but ping the Arduino without any other distractions and see if that behaves more reliably?
採用された回答
その他の回答 (2 件)
Image Analyst
2025 年 7 月 24 日
0 投票
See if the problem goes away if you set the priority of any of your program's processes to High. Type Control-Shift-Esc to bring up Task manager. On the "Details" panel (Window 11, slightly different in Windows 10) find your processes (any MATLAB processes, your executable, and anything else related to your running of your program. Then right click and go to "Set priority" and set it to High. See if that avoids the problem.

1 件のコメント
Raising priorities was discussed early on, IA, I do not know whether Gavin actually experimented with doing so or not, however...
It would still be interesting to see if could somehow tie in what is happening with OS context switching at the same time these delays occur...
カテゴリ
ヘルプ センター および File Exchange で MATLAB Support Package for Arduino Hardware についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





