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

3 views (last 30 days)
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
Marek
Marek on 4 Apr 2025
Edited: Marek on 6 Apr 2025
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(...")).
Sam Marshalik
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:
  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

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!