简体   繁体   English

gcc和g ++在C程序之间的性能差异

[英]Performance difference between gcc and g++ for C program

Lets say i have written a program in C and compiled it with both gcc and g++, which compilation will run faster? 可以说我已经用C语言编写了一个程序,并同时使用gcc和g ++对其进行了编译,哪种编译速度会更快? gcc or g++? 是gcc还是g ++? I think g++ compilation will make it slow, but not sure about it. 我认为g ++编译会使其变慢,但不确定。

Let me clarify my question again because of confutation about gcc. 由于对gcc的争议,让我再次澄清我的问题。

Let say i compile program ac like this on console. 假设我在控制台上像这样编译程序ac。

gcc a.c

g++ a.c

which a.out will run faster? 哪个a.out将运行得更快?

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. 首先:这个问题(和其他一些答案)似乎是基于这样一个错误的前提,即C是C ++的严格子集,实际上并非如此。 Compiling C as C++ is not the same as compiling it as C: it can change the meaning of your program! 将C编译为C ++ 不同于将其编译为C:它可以更改程序的含义!

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. C大部分将像C ++一样进行编译,并且大多数将产生相同的结果,但是有些东西被明确定义以给出不同的行为。

Here's a simple example - if this is your ac : 这是一个简单的示例-如果这是您的ac

#include <stdio.h>

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

then compiling as C will give one result: 然后编译为C将得到一个结果:

$ 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): 并以C ++进行编译会得到不同的结果(除非您使用的是intchar大小相同的不寻常平台):

$ 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 . 因为C规范将字符文字定义为int类型,而C ++规范将字符文字定义为char类型。

Secondly: gcc and g++ are not "the same compiler". 其次: gccg++不是“同一编译器”。 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). 使用了相同的后端代码,但C和C ++前端是不同的代码段( gcc/c-*.cgcc/cp/*.c )。

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 (ie giving exactly the same input to the back end), and hence no guarantee that the generated code will be identical. 即使您坚持使用语言中定义为执行相同操作的部分,也无法保证C ++前端将以与C前端完全相同的方式来解析代码(即,将完全相同的输入提供给后端),因此不能保证所生成的代码是相同的。 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. 两者之间的差异可能很大。

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. 如果您想找出答案,则可以为两者编译程序集并进行比较,但是我敢打赌,它们会创建相同的程序集,因此会创建相同的机器代码。

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... 虽然如果您在C代码中没有extern C {} ,或者在C中工作正常,我不确定“好像是C ++一样进行编译”可以提供任何速度,除非g ++中进行了特定的编译器优化刚好适合您的特定情况...

The machine code generated should be identical. 生成的机器代码应相同。 The g++ version of a.out will probably link in a couple of extra support libraries. a.out的g ++版本可能会链接几个额外的支持库。 This will make the startup time of a.out be slower by a few system calls. 通过一些系统调用,这将使a.out的启动时间变慢。

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. 在达到20-40个链接的库和成千上万个要解析的符号之前,Linux链接器不会变得明显变慢。

The gcc and g++ executables are just frontends, they are not the actual compilers. gcc和g ++可执行文件只是前端,它们不是实际的编译器。 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. 它们都基于文件扩展名运行实际的C或C ++编译器(以及ld,ar,以及生成所需输出的任何内容)。 So you'll get the exact same result. 这样您将获得完全相同的结果。 G++ is commonly used for C++ because it links with the standard C++ library (iostreams etc.). G ++通常用于C ++,因为它与标准C ++库(iostream等)链接。

If you want to compile C code as C++, either change the file extension, or do something like this: 如果要将C代码编译为C ++,请更改文件扩展名或执行以下操作:

gcc test.c -otest -x c++

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

GCC is a compiler collection. GCC是一个编译器集合。 It is mainly used for compilation of C,C++,Ada,Java and many more programming languages. 它主要用于C,C ++,Ada,Java和许多其他编程语言的编译。 G++ is a part of gnu compiler collection(gcc). G ++是gnu编译器集合(gcc)的一部分。
I mean gcc includes g++ as well. 我的意思是gcc也包括g ++。 When we use gcc for compilation of C++ it uses g++. 当我们使用gcc编译C ++时,它使用的是g ++。 The output files will be different because the G++ compiler uses its own run time library. 输出文件将有所不同,因为G ++编译器使用其自己的运行时库。

Edit: Okay, to clarify things, because we have a bit of confusion in naming here. 编辑:好的,要澄清一下,因为在这里命名时有些混乱。 GCC is the GNU Compiler Collection. GCC是GNU编译器集合。 It can compile Ada, C++, C, and a billion and a half other languages. 它可以编译Ada,C ++,C以及数十亿种其他语言。 It is a "backend" to the various languages "front end" compilers like GNAT. 它是GNAT等各种语言“前端”编译器的“后端”。 Go read the link i made at the top of the page from GCC.GNU.Org. 从GCC.GNU.Org中阅读我在页面顶部创建的链接。

GCC can also refer to the GNU C Compiler. GCC也可以参考GNU C编译器。 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. 如果给出-lstdc ++命令,它将编译C ++代码,但通常会窒息而死,因为它没有引入C ++库。

G++, the GNU C++ Compiler, like the GNU C Compiler is a front end to the GNU Compiler Collection. 与GNU C编译器一样,G ++,GNU C ++编译器也是GNU编译器集合的前端。 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. C编译器之间的区别在于,它自动包含了这些库并进行了一些其他小的调整,因为它假定将要馈送C ++代码进行编译。

This is where the confusion comes from. 这就是混乱的源头。 Does this clarify things a bit? 这会澄清一些事情吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM