Same script, faster execution when running in a spawned instance ("!matlab ...")?

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

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.
Thank you for your comment. I will look into the linked text.
I suspected something related to GUI/noGUI/keyboard etc. issues, but have no explanation for the phenomenon.
1) The structure of the "script" version is the following:
"my-main.m" file (script) runs a "for" loop, which is stepping through datasets. For each dataset a few lines within the loop set up variables (v1 ... ), which are then passed to the actual "engine" doing the analysis. The "engine" code ("my-engine"), is embedded in the "my-main.m" file as a function:
file my-main.m:
% File begin
for i = 1 : NoOfDatasets
v1 = Value1;
v2 = Value2;
v3 = Value3;
my-engine(v1, v2, v3);
end
function my_engine(varargin)
engine body
end
% File end
2) The "spawned" version does in principle the same, but instead of jumping to the embedded function a new matlab instance is started, which then runs the function "my-engine" (variables being passed via *.mat file), kept in an external file:
file my-main-spawn.m:
% File begin
for i = 1:NoOfDatasets
v1 = Value1;
v2 = Value2;
v3 = Value3;
save('instance_v.mat','v1','v2','v3');
!matlab -nosplash -desktop -r "load instance_v.mat; my-engine(v1, v2, v3)"
end
%File end
file my-engine.m:
% File begin
function my_engine(varargin)
engine body
% File end
3) I have also tried an intermediate version:
"my-main.m" file (script) calling the "engine" from an external function file (without initiating a separate matlab instance):
file my-main.m:
% File begin
for i = 1:NoOfDatasets
v1 = Value1;
v2 = Value2;
v3 = Value3;
my-engine(v1, v2, v3);
end
% File End
file my-engine.m:
% File begin
function my_engine(varargin)
engine body
end
% File end
The execution times of the three scenarios differ significantly, with spawned being the fastest and intermediate (ver. 3) the slowest of them.
Still have no clue - ?
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
Yes, I do mean "-desktop -r", i.e. the instance started visible and interactive (in case manual interevention were desired).
[EDIT]
By the way, I notice no difference (<1%) in execution speed between "-desktop" and "-nodesktop" instances.
[/EDIT]
Although "-r" is not recommended (and "-batch" shall be used instead) it does not seem to do any harm, as the code in the spawned instance completes without problems AND a lot faster than the same code in the main Matlab session.
I have now tested the above mentioned scenarios on serveral different datatsets and I am still getting the same behavior: spawned instance completing the code faster.
In case this might be of any relevance: my code (be it script or function) calls several other functions, partly my own (kept in separate myfunction.m files), as well as ML intrisic ones (e.g. "find(...")).
It sounds like both instances of MATLAB are using similar hardware resources. I'd suggest to test the following:
  1. 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.
  2. 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?

Sign in to comment.

Answers (0)

Categories

Products

Release

R2024b

Asked:

on 2 Apr 2025

Commented:

on 5 May 2025

Community Treasure Hunt

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

Start Hunting!