C++ taking much time than C in first run of hello world

Hello all,

I have just started learning C++, but I have good knowleg of C.
when running the simple code "Hello World!" on C++ and C starting by an empty file, I found the following :

- C++ : when I build the code and after that run it for the first time it takes
almost 2.5 sec and the next run takes around 0.03

- C : when I do the same thing with C first run takes 0.3 sec and the next run 0.022 sec !

So is this a normal behavior? otherwise, what could be the probleme, I thought C++ is at least as fast as C but I found the opposet here !

Thank you.
Show your code, both the C and C++ code.

Are you talking about compile time or runtime?

If runtime how are you running the programs.

What operating system, compiler, etc.



The difference between 0.03 and 0.022 is noise.

As for the first run, there are likely a myriad of caches involved.
- how much of your program was still in the disk cache following your build.
- how many of the shared libraries your program depends on are currently resident.

Your OS / Compiler / disk (especially the spinning variety) / memory / ..... all play a part.

Assessing the performance of something is statistical, and you would typically exclude obvious out-liers like 2.5 seconds as being some system anomaly outside of the control of your program.
I am using CodeBlooks on windows 10. inter cor i5 8th gen, 8GB ram , with SSD.
The time the I have mentioned is the execution time which shows up on the cmd window when I run the code.

what I am doing is just :
1- save > build > run> takes 2.5 sec ....
2- then close cmd and again run > takes 0.03

It is moslty the compile time, because when I run the program the first time it take so much time relatively. and less when run again without any change

This shows up with C code also ; first run takes more time than the second one. but way more less than C++ case.


This is the C version :

1
2
3
4
5
6
#include <stdio.h>
int main() {
   printf("Hello, World!");
   return 0;
}


and this is the C++ version :
1
2
3
4
5
6
7
#include <iostream>

int main() {
    std::cout << "Hello World!";
    return 0;
}

Last edited on
Well since the "first time" seems to also include compile time, I would expect the C program to compile faster, a lot of the iostream features will need to be compiled into the program. The stdio functions don't need to be compiled, they just need to be linked into the program.



Then it's a normal behavior?

Will it take more time when compiling larger codes with c++ or it will just add the required time of the "iostream"?

I am asking because I'm about to start devloping a biger code in fluid dynamics. and I need to change things in the code now and then, or maby many times.
I'm affraid that it will make some trubles during the process.
The first run will always take longer, because there is less already loaded into memory. It's not a compile-time delay. The same happens just running the executable. Input and output are also inherently slow - they aren't representative of general numerical operations.

If you don't have fancy data structures then C will run (slightly) faster, simply because it's a smaller language. If you do have data structures (particularly dynamically-sized containers) then C++ has these built into the language, so will probably improve on anything you can code. C++ is also more flexible to code and develop. It depends on how time-consuming your code development is. If you are writing a big fluids code then what you do about parallelisation is likely to far outweigh the differences between C and C++.

Last edited on
this isnt much of a test as noted above in the explains, but
I thought C++ is at least as fast as C but I found the opposet here !


there are too many variables. I can write a c program that is faster than the similar c++ program. I can write a c++ program that is faster than a similar C program. It depends on what exactly is being done, what compiler and OS is being used, compiler flags, and more. You cannot make any kind of generalization, but if you do need to make such a generalization, about the best you can say is "they tend to be very close when doing *exactly* the same things". Exactly the same things is tricky, as C does not have STL so you would have to write a similar library for what you used in the C++ program or write the c++ as "C with minor enhancements" style and not use the stl or anything modern.

It is moslty the compile time,
Yes, C++ generally does take longer to compile, because of its use of templates and more-complicated syntax.

But as far as runtime, the general rule is that you only pay for what you use. (The exception to this is RTTI and exception handling, which has some minor overhead even if not used, which is why most compilers allow them to be turned off.)
Last edited on
Topic archived. No new replies allowed.