tags:

views:

234

answers:

6

Lets say i have written a program in C and compiled it with both gcc and g++, which compilation will run faster? gcc or g++? I think g++ compilation will make it slow, but not sure about it.

Let me clarify my question again because of confutation about gcc.

Let say i compile program a.c like this on console.

gcc a.c

g++ a.c

which a.out will run faster?

+2  A: 

I think they they will both produce the same machine code, and therefore the same speed on your computer.

If you want to find out, you could compile the assembly for both and compare the two, but I'm betting that they create the same assembly, and therefore the same machine code.

KLee1
What i am thinking is g++ will generate more instruction in assembly, so may take more time that gcc?
itsaboutcode
@itsaboutcode: *Why?* Just run it twice and compare the output if you're that worried.
GMan
I'm convinced that there's an inverse relationship between programming experience and level of concern about optimization.
Tyler McHenry
@Tyler: +1 Thanks for the laugh! I wonder if there is also a similar relationship for fascination with `x = x++ + --x` and other UB nonsense like that.
Amardeep
Myth: Premature optimization is the root of all evil.Truth: The LOVE of premature optimization is the root of all evil. :)
Stabledog
+7  A: 

Zero difference. Same compiler.

http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/G_002b_002b-and-GCC.html

GCC is a compiler collection. It is mainly used for compilation of C programms. Gcc can be used for other languages also.
G++ is a part of gnu compiler collection(gcc).
I mean gcc includes g++ as well. When we use gcc for compilation of C++ it uses g++ internally if using the -lstdc++ flag. So there should be zero difference between the two compiled executables in your example, if you're using GNU GCC/G++.

Edit: Okay, to clarify things, because we have a bit of confusion in naming here. GCC is the GNU Compiler Collection. It can compile Ada, C++, C, and a billion and a half other languages. It is a "backend" to the various languages "front end" compilers like GNAT. Go read the link i made at the top of the page from GCC.GNU.Org.

GCC can also refer to the GNU C Compiler. This will compile C++ code if given the -lstdc++ command, but normally will choke and die because it's not pulling in the C++ libraries.

G++, the GNU C++ Compiler, like the GNU C Compiler is a front end to the GNU Compiler Collection. It's difference between the C Compiler is that it automatically includes those libraries and makes a few other small tweaks, because it's assuming it's going to be fed C++ code to compile.

This is where the confusion comes from. Does this clarify things a bit?

Caladain
I've seen them produce different assembly on the same C code.
James Roth
That's not quite true. Sections .dynstr, .rodata, and .eh_frame are significantly different in my trivial test program. The executable code itself has an extra trampoline (_gxx_personality_v0@plt) and orders some things slightly differently in the g++ version. It won't have a significant impact on load time, but it isn't identical.
Nathon
According to the documentation, GNU GCC and GNU G++ are one and the same. If they're producing different code, then i'll investigate and forward that info to the FSF to have the documentation revisited.
Caladain
Besides the data sections, I've seen actual code change. And I don't see where they claim that will not happen.
James Roth
-1: C and C++ are different languages. Even if gcc and g++ would be the same program (which they aren't, they are two different frontends) they *must* compile some things differently if the semantics of the code (the interpretation in C or C++) are different. Whether or not this makes a significant difference only a benchmark can tell.
Jens Gustedt
+2  A: 

Profile it and try it out. I'm certain it will depend on the actual code, even if it would require potentially a really weird case to get any different bytecode. Though if you don't have extern C {} around your C code, and or works fine in C, I'm not sure how "compiling it as though it were C++" could provide any speed, unless the particular compiler optimizations in g++ just happen to be a bit better for your particular situation...

eruciform
+2  A: 

The machine code generated should be identical. The g++ version of a.out will probably link in a couple of extra support libraries. This will make the startup time of a.out be slower by a few system calls.

There is not really any practical difference though. The Linux linker will not become noticeably slower until you reach 20-40 linked libraries and thousands of symbols to resolve.

Zan Lynx
+1  A: 

The gcc and g++ executables are just frontends, they are not the actual compilers. They both run the actual C or C++ compilers (and ld, ar, whatever is needed to produce the output you asked for) based on the file extensions. So you'll get the exact same result. G++ is commonly used for C++ because it links with the standard C++ library (iostreams etc.).

If you want to compile C code as C++, either change the file extension, or do something like this:

gcc test.c -otest -x c++
torhu
So what you are saying if i try to compile c code as c++, it may take more time than c code compilation?
itsaboutcode
I don't know. But since the C++ compiler naturally has to be more complex, that seems likely. Would probably take a lot of code to actually see a difference, though. You could try it out.
torhu
man gcc: *"g++ is a program that calls GCC and treats .c, .h and .i files as C++ source files instead of C source files unless -x is used"* so this is wrong.
Cirno de Bergerac
Sorry, it seems g++ actually defaults to C++ mode for .c files. Other than that my explanation is correct according to some quick testing I just did with MinGW 3.4.5. Then again, this could be just because Windows uses case-insensitive filename matching, and since .C (capital C) is used for C++ code.
torhu
+6  A: 

Firstly: the question (and some of the other answers) seem to be based on the faulty premise that C is a strict subset of C++, which is not in fact the case. Compiling C as C++ is not the same as compiling it as C: it can change the meaning of your program!

C will mostly compile as C++, and will mostly give the same results, but there are some things that are explicitly defined to give different behaviour.

Here's a simple example - if this is your a.c:

#include <stdio.h>

int main(void)
{
    printf("%d\n", sizeof('x'));
    return 0;
}

then compiling as C will give one result:

$ gcc a.c
$ ./a.out
4

and compiling as C++ will give a different result (unless you're using an unusual platform where int and char are the same size):

$ g++ a.c
$ ./a.out
1

because the C specification defines a character literal to have type int, and the C++ specification defines it to have type char.

Secondly: gcc and g++ are not "the same compiler". The same back end code is used, but the C and C++ front ends are different pieces of code (gcc/c-*.c and gcc/cp/*.c in the gcc source).

Even if you stick to the parts of the language that are defined to do the same thing, there is no guarantee that the C++ front end will parse the code in exactly the same way as the C front end (i.e. giving exactly the same input to the back end), and hence no guarantee that the generated code will be identical. So it is certainly possible that one might happen to generate faster code than the other in some cases - although I would imagine that you'd need complex code to have any chance of finding a difference, as most of the optimisation and code generation magic happens in the common back end of the compiler; and the difference could be either way round.

Matthew Slattery