Same script, faster execution when running in a spawned instance ("!matlab ...")?
Show older comments
Dear Community,
I am running Matlab 2024b under Windows 10 / 32GB RAM on a 6-core laptop Intel CPU (hyperthreading deactivated). For the sake of runtime optimisation I have done the following comparison:
case #1: a script running within the current Matlab session (single instance) and
case #2: the same script running in a spawned session (instance), using "!matlab -nosplash -desktop -r "load ..." (etc.).
The runtime results are as follows:
script #1 finished in about an hour, total CPU load ~50% at boost clock (single Matlab instance)
script #2 finished in about HALF an hour (!), similar total CPU load ~50% at boost clock; main Matlab instance idle (~0%), spawned instance ~40%
I muss admit I do not understand the results: why does a spawned Matlab instance (i.e. two instances running in parallel: main/idle and active) complete the task in half of the time, compared to a single instance?
What might be the reason for this behavior?
Thanks a lot in advance!
Marek
5 Comments
dpb
on 2 Apr 2025
Seems unreasonable on the surface, yes...do that several more times and it may be done before you even begin! :J> I would have no explanation other than there has to be something in the interactive session...it possibly is polling the keyboard or somesuch that the batch process doesn't do.
One serious observation, however...
When you say "script" do you really mean a script and not a function? If it really is a script, turning it into a user function may be able to help run time significantly. See <this Answers thread for discussion why> if this is new to you.
Marek
on 3 Apr 2025
Edited: Walter Roberson
on 3 Apr 2025
Mike Croucher
on 4 Apr 2025
Not sure what's causing the speed difference but do you really mean
!matlab -nosplash -desktop -r
and not
!matlab -nosplash -nodesktop -r
Either way, I'd suggest not using either of these and launching batch MATLAB with the newer
!matlab -batch
More details on the -batch switch at https://uk.mathworks.com/help/matlab/ref/matlabwindows.html
Sam Marshalik
on 5 May 2025
It sounds like both instances of MATLAB are using similar hardware resources. I'd suggest to test the following:
- Run "!matlab -nosplash -desktop -r "load ..." (etc.)" outside of MATLAB and just via Command Prompt. I do not think running this in MATLAB should cause any difference vs. running this via command line. Is performance similar to that of kicking it out of MATLAB? You will want to use -nodesktop, though.
- Try profiling both the interactive and the command line running instances to see where the time is spent. Is some particular part of the code taking longer to run?
Answers (0)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!