Why does it take MATLAB so long to print hello world?
30 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Jon
am 15 Sep. 2023
Bearbeitet: Jon
am 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.
Antworten (3)
Image Analyst
am 6 Sep. 2023
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 Kommentare
Jon
am 11 Sep. 2023
Thanks Matthew, I very much appreciated your thoughtful response regarding keeping it friendly.
Jon
am 6 Sep. 2023
Bearbeitet: Jon
am 6 Sep. 2023
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.
John D'Errico
am 6 Sep. 2023
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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!