Why does it take MATLAB so long to print hello world?
Show older comments
I apologize in advance for the formatting -- this forum post editor does not conform to the CommonMark standard, and thus I cannot properly share non-MATLAB code snippets. A distinct font will have to do until MathWorks adds support for basic Markdown features. EDIT: Somebody ninja-edited my question to get rid of the distinct font. No font will have to do, now. Seriously -- how do you guys discuss anything involving terminal use in this forum???
It takes MATLAB ~2.6 seconds to print "Hello, World!" from the command line:
$ time matlab -nojvm -noFigureWindows -nodisplay -batch "disp('Hello, World!')" Hello, World!
real 0m2.601s
user 0m2.645s
sys 0m0.267s
For comparison, that's 90x longer than the time it takes to read from stdin, compile, run, delete, and echo to stdout the equivalent C program:
$ time echo $(gcc -xc - <<< '#include "stdio.h"
int main() { printf("Hello, World!\n"); return 0; } ' && ./a.out && rm a.out) Hello, World!
real 0m0.029s
user 0m0.025s
sys 0m0.004s
It's also 170x longer than it takes to perform the Python equivalent:
$ time python3 -c "print('Hello, World!')"
Hello, World!
real 0m0.015s
user 0m0.012s
sys 0m0.004s
Why does MATLAB seem to take hundreds of times longer than it needs to in order to run a simple "Hello, World!" application?
9 Comments
the cyclist
on 6 Sep 2023
What is your use case for which this matters? Often these sorts of questions have better solutions.
Even more often, these sorts of questions are just bad-faith slams against MATLAB, with no actual interest in solving a problem.
Matthew Elmer
on 6 Sep 2023
Sam Marshalik
on 7 Sep 2023
@Matthew Elmer: I wanted to dig in a bit into this comment:
"Before you ask, the (Monte-Carlo) simulations indeed ran long enough for the prospective benefits of multithreading to outweigh the overhead."
By default, parallel workers are single-threaded, so if your code utilizes multi-threading, you will want to potentially change the number of threads (NumThreads) that the parallel workers have access to.
Matthew Elmer
on 7 Sep 2023
Sam Marshalik
on 7 Sep 2023
@Matthew Elmer: I can clarify what that means. We recommend a 1 to 1 ratio between the number of parallel workers you strart and the number of physical cores available on the machine. So, if you have an 8 core CPU, we would recommend to start a pool of 8 workers. If you do not specify, I believe MATLAB should start a pool with a size that matches your number of physical cores (like the quote you mentioned). I always encourage people to specify their pool size so there are no surpises.
When working with workers and threads, follow this recipe:
Number of workers * number of threads that each worker has access to =< Number of Physical Cores
For example, if I have a parfor loop that includes a multi-threaded function, I could run multiple parfor iterations at the same time, by starting a pool of 2 workers and giveeach worker access to 4 threads:
>> c = parcluster("Processes");
>> c.NumThreads = 4;
>> c.parpool(2)
Hopefully that helps clarify things.
Matthew Elmer
on 9 Sep 2023
Walter Roberson
on 11 Sep 2023
To insert non-MATLAB code in postings, as code for people to be able to use the Copy button to copy the code to execute on their own machines:
Click on the editor '>' toolbar button, in the 'CODE' section of the toolbar. Paste the code in to the region the editor created. If the editor automatically indented the code and you do not want the automatically formatted version, then after the paste, press control-Z : that will undo the automatic formatting.
To insert anything in postings as a mix of code and output for people to look at, without the expectation that they will want to copy it for execution on their own machines:
Click on the rightmost icon in the toolbar 'INSERT' section -- the one that looks like a stack of 4 lines. Paste the code/output in to the region the editor created. If it automatically formatted in a way you do not like, then press control-Z to undo the formatting.
Note: if you have already entered code into the editor as regular text, then you can highlight the code with your mouse, and then click on the '>' button and the text will be converted to a code region; you might need to control-Z to undo formatting.
Matthew Elmer
on 15 Sep 2023
@Matthew Elmer While I understand that you would like to be able to more easily post nicely formatted code in other languages, I think the currently implemented "Code" button is mainly intended for people to post MATLAB code as this is the focus of MATLAB answers.
I haven't really seen too many instances where people have had questions where it was important to them to post nicely formatted code in other computer languages. So if this is short coming of the MATLAB Answers site, I don't think it affects that many users.
I find sites like Stack Overflow are more oriented to general programming issues and go there for non MATLAB specific questions.
Answers (3)
Image Analyst
on 6 Sep 2023
2 votes
What MATLAB commands did you issue from the command line? What you gave does not work:
>> $ time matlab -nojvm -noFigureWindows -nodisplay -batch "disp('Hello, World!')"
$ time matlab -nojvm -noFigureWindows -nodisplay -batch "disp('Hello, World!')"
↑
Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII
characters.
>> time matlab -nojvm -noFigureWindows -nodisplay -batch "disp('Hello, World!')"
Incorrect number or types of inputs or outputs for function 'time'.
Was it from a console window prompt instead of the command prompt in MATLAB? If it was, it looks like you may be launching the full MATLAB program, just with no user interface shown, which of course will take more time than if MATLAB is already up and running. You will of course have lots of overhead in that case. Maybe you should test out some longer snippet of code that takes much longer to compute so the overhead is not a significant part of the total time. When I do it in MATLAB I get this on my old, slow computer:
Hello, World!
Elapsed time is 0.000162 seconds.
>>
To format your code as code, you highlight the code with the mouse and then click the Code icon on the Answers edit box tool ribbon. Pretty simple.
11 Comments
Image Analyst
on 6 Sep 2023
@the cyclist thanks for clarifying. I have Windows. I still think most of the time is taken up not by disp() but by the overhead of launching MATLAB as a program with no GUI.
the cyclist
on 6 Sep 2023
Sorry I deleted the comment that @Image Analyst is replying to, because upon further reflection I felt that it was unnecessary.
The gist was that I assume @Matthew Elmer was launching that command from a Mac terminal, not the MATLAB command line.
Matthew Elmer
on 6 Sep 2023
Matthew Elmer
on 6 Sep 2023
Image Analyst
on 6 Sep 2023
That's not the point. Perhaps launching MATLAB with no GUI is faster than without a GUI. But who cares? When running a large program, as opposed to s single line, you don't care about the overhead to launch the program - that overhead should not be part of the benchmark timing. The point I'm making is that using a single line of code to display a single line of text is not a valid benchmark to compare the speed of MATLAB with Python because your benchmark code is such a small part of the total time, which includes the overhead of the Operating System launching the main program. Heck, even with MATLAB running, and (say) Jupyter Notebook running, so there is no overhead of launching the main program, using a single line of code to compare speeds of various languages is not a valid comparison. Here is one comparison that shows MATLAB is faster:
Walter Roberson
on 6 Sep 2023
In my opinion, if you have a bunch of independent tasks to accomplish, it is valid to ask "How long does it take to get them all done?" . Startup time plus computation time counts.
If you are in a situation where MATLAB needs to be started a lot, then it is probably time to consider using MATLAB Production Server
Matthew Elmer
on 7 Sep 2023
Edited: Matthew Elmer
on 7 Sep 2023
Matthew Elmer
on 7 Sep 2023
Jon
on 7 Sep 2023
@Matthew Elmer One reason I really like getting and giving help on MATLAB answers is it is generally a very friendly site. I realize that sometimes passion and enthusiasm for a topic may mistakenly be taken for sounding angry, so if I am misreading your tone please forgive me. But, if not, if you can try to keep it friendly and constructive I would appreciate it. Thanks
Matthew Elmer
on 9 Sep 2023
Jon
on 11 Sep 2023
Thanks Matthew, I very much appreciated your thoughtful response regarding keeping it friendly.
I'm not sure how you are running your test, running this script inside this online environment seems to indicate it is very fast compared to what you report
tic
disp('Hello World!')
toc
When I just type on the command line on my laptop, it is even faster
tic,disp('Hello World'),toc
Hello World
Elapsed time is 0.000805 seconds.
1 Comment
Matthew Elmer
on 6 Sep 2023
John D'Errico
on 6 Sep 2023
0 votes
To me, this all seems to be making a mountain out of a mole hill.
Yes, if you are going to launch MATLAB, even with no gui interface, MATLAB will surely cache all the functions it uses. It will do lots of crapola that you don't care about, IF your only goal is to display a line of text, and then quit. If that is what you will do on your computer, then you need ot be using a different tool, not MATLAB. That Python (or C) is faster to display a line of text, good for Python. Use it instead.
In your case, it appears that you want to use MATLAB as you are trying to use it, AND that the overhead of booting MATLAB every time is high. If you compare any of those anguages, they all have their advantages, and disadvantages. As @Image Analyst points out, for at least some operations MATLAB is faster than Python. (I can also point out one specific case that I know of where Python wins the battle by a mile. But I also have a workaround in MATLAB.)
I'm sorry, but no language is perfect. They all live under their own sets of fundamental assumptions that influence what they will do well.
1 Comment
Matthew Elmer
on 7 Sep 2023
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!