Code in live script run much slow
110 views (last 30 days)
The new matlab live script functionality is really awesome, thanks matlab team.
But I observe that my code run considerably slower in live script than in the command line. Just out of curiousity, is this an expected result for a newly created function or there is some way to configure the amount of resource allocation for live script? Thanks.
Philip on 5 Mar 2020
Edited: Philip on 14 Oct 2020
This issue has been partially resolved in R2020b, R2020a, R2019b Update 5, and R2019a Update 8. Most Live scripts with loops now run as fast in the Live Editor as in the Editor or at the command line.
For example, with this change, the approximate execution times of the example from Lukas Müller's post above are:
R2018b: 200.2 s
R2020a: 0.160 s
The code was timed on a Windows 10 system with a 3.6 GHz Intel Xeon W-2133 CPU.
However, as mentioned in the replies below, the performance of rendering graphics within the Live Editor is still a known issue. Specifically, figures containing many graphics objects, such as those often created by basic graphics commands (plot, line, etc.) in tight loops, can be impacted.
For example, this code (based on the post by jdg below), takes 12 seconds to run in the R2020b Live Editor, and 4 seconds in the R2020b Command Window. The code was timed on a Windows 10 system with a 3.6 GHz Intel Xeon W-2133 CPU.
for i = 1:2700
plot(rand, rand, 'ob');
This issue is actively being worked on. As a workaround, code that creates figures with many graphics objects can often be rewritten to reduce the number of graphics objects being created. This change can improve the overall performance of the code. For example, vectorizing the graphics code or using a different graphics command such as “scatter” can reduce the run time of the code in the Live Editor to be almost equal to the run time in the Command Window.
Just as an example, when the code above is updated to gather the data first and only plot the data once, the code now takes 0.5 seconds to run in the R2020b Live Editor, and 0.45 seconds in the R2020b Command Window. The code was timed on a Windows 10 system with a 3.6 GHz Intel Xeon W-2133 CPU.
x = zeros(1, 2700);
y = zeros(1, 2700);
for i = 1:2700
x(i) = rand;
y(i) = rand;
scatter(x, y, , 'ob');
If you have any other questions, or other use cases that you would like us to better consider, please feel free to reach out to Technical Support to let us know.
More Answers (7)
Gijs van Oort on 31 May 2017
The most annoying I think, is that editing is much slower too. After pressing a key, it takes noticeable time for the character to be shown on the screen. Also walking through the code with the arrow keys and scrolling are really slow. Due to this, I'm now ending up writing my code in a normal .m script and copying it to the .mlx when I'm happy with the results.
Prannay Jain on 15 Nov 2016
Yes, the code runs slower in the live script than in the command line. Currently, there is no way to configure the amount of resource allocation for the live script.
I work for MathWorks and I have forwarded this feedback to the appropriate product team.
Tom Anderson on 9 Jan 2017
Edited: Tom Anderson on 16 Jan 2017
Please improve this! It's almost 50% slower to run .mlx versus .m...
I ran a code section for a live file using "Run Section" that took 72.971023 seconds. The same code .m file using "Run Section" took 52.701320 seconds. Repeated this test multiple times and .mlx is always a loser.
That's not very good. It's almost a 50% slowdown. It's not as though there's any extra interactivity between the code, either. Just waiting for the results both times, with the GUI locked up.
Florian Blanchet on 11 Dec 2019
Can you remove that feature or at least make it clear that is significantly slower and will reduce editor experience ?
How can you provide a feature that is so unusable without make it explicit it is still experimental ?
Frieder Wittmann on 28 May 2018
Has this been addressed in R2018a? Didn't see anything in the release notes.
Juan José Trujillo on 17 Sep 2018
I do not know what is the motivation for you to use the live scripts. I really find this a cool way of documenting and something which approaches a bit what you can do in Jupyter Notebooks.
In fact, I have been using successfully Jupyter Notebooks with the matlab engine in the last two years to run matlab code. I have worked that way since I have some python background and did not know live scripts until recently. Just lately I have got in touch with matlabs live scripts through a colleague and we have noticed latency which I do not experience through Jupyter Notebooks. I guess the mechanics here are really different and the Notebook approach almost resembles running bare scripts with a bit of overhead on the matlab engine.
Maybe you can give Jupyter Notebook a try and get better performance and with slightly better documentation capabilities with markdown.