Why does it take MATLAB so long to print hello world?

14 views (last 30 days)
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
Matthew Elmer
Matthew Elmer on 15 Sep 2023
@Walter Roberson Please post an example that includes correctly-syntax-highlighted Python, along with an example that contains simple monospace text without paragraph spacing between newlines.
Jon
Jon on 15 Sep 2023
Edited: Jon 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.

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 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 Comments
Matthew Elmer
Matthew Elmer on 9 Sep 2023
My apologies if my tone came across as angry. I really would like to come across as friendly, but I cannot compromise on calling people out on responses that are actively counterproductive to users with time/resource constraints and the computer programming industry at large. The benchmarks that I shared show that MATLAB could be a lot faster on headless startup and thus a lot more valuable to more users. If I have to contend with an avalanche of excuses, especially ones like this that don't match the friendly tone you're requesting either, I'm going to have to compromise on tone a bit for the sake of clarity.
Your criticism is well taken, though, and I'll try even harder to keep the atmosphere polite.
Jon
Jon on 11 Sep 2023
Thanks Matthew, I very much appreciated your thoughtful response regarding keeping it friendly.

Sign in to comment.


Jon
Jon on 6 Sep 2023
Edited: Jon on 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!')
Hello World!
toc
Elapsed time is 0.015690 seconds.
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
Matthew Elmer on 6 Sep 2023
My timing was done in a Bash terminal. Try running the Python command or compiling a C program and you'll see that it's not the same as MATLAB's REPL.

Sign in to comment.


John D'Errico
John D'Errico on 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.
  1 Comment
Matthew Elmer
Matthew Elmer on 7 Sep 2023
It's not my choice to use MATLAB. I'm merely accomodating those who don't have the time to learn something better. After seeing how much time they spend sitting around, no wonder they don't have any left for learning a better language!

Sign in to comment.

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!