Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My hello world c++ program takes about 1.5 seconds to run, is that normal?

This is my code:

#include <iostream>
using namespace std;
int main() 
{
    cout << "Hello, World!";
    return 0;
}

Picture of my screen. Isn’t c++ supposed to be extremely fast? Why is my code taking this long to run? I’m running on Visual Studio Code, Windows 10, 6GB RAM.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To be more specific: What you’re doing actually has three steps:

You move to a specific directory (via the cd command). Then we have code compilation (calling g++). Lastly we have running the actual executable (named test in your picture).

It’s likely that running just the executable would yield a lower time than all three steps you have in the screenshot.

If the executable is still slow, consider trying an optimization flag for g++ as found here: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html. These flags can make compilation slower, but the actual execution faster.

For fun I timed your example code on WSL using the time command:

csm10495@csm10495-desk:/mnt/c/Users/csm10495/Desktop $ time g++ test.cpp -o test

real    0m0.235s
user    0m0.117s
sys     0m0.043s
csm10495@csm10495-desk:/mnt/c/Users/csm10495/Desktop $ time ./test
Hello, World!
real    0m0.005s
user    0m0.002s
sys     0m0.000s

So for me the executable runs in .005s, while the compile took .235s.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading