简体   繁体   English

由编译的编译器执行编译的代码

[英]Performance of compiled code by compiled compiler

If I want to achieve better performance from, let's say for example, MySQLdb, I can compile it myself and I will get better performance because it's not compiled on i386, i486 or what ever, just on my CPU. 如果我想从中获得更好的性能,比如说MySQLdb,我可以自己编译它,我会得到更好的性能,因为它不是在i386,i486上编写的,只是在我的CPU上。 Further I can choose the compile options and so on... Now, I was wondering if this is true also for non-regular Software, such as compiler. 此外,我可以选择编译选项等等......现在,我想知道对于非常规软件(例如编译器)是否也是如此。 Here come the 1st part: 这是第一部分:

  • Will compiling a compiler like GCC result in better performance? 编译像GCC这样的编译器会产生更好的性能吗? and the 2nd part: 第二部分:
  • Will the code compiled by my own compiled compiler perform better? 我自己编译的编译器编译的代码会更好吗?

(Yes, I know, I can compile my compiler and benchmark it... but maybe ... someone already knows the answer, and will share it with us =) (是的,我知道,我可以编译我的编译器并对其进行基准测试......但也许......某人已经知道了答案,并将与我们分享=)

In answer to your first question, almost certainly yes. 在回答你的第一个问题时,几乎肯定是的。 Binary versions of gcc will be the "lowest common denominator" and, if you compile them with special flags more appropriate to your system, it will most likely be faster. gcc二进制版本将是“最低标准”,如果使用更适合您系统的特殊标志编译它们,它很可能会更快。

As to your second question, no. 至于你的第二个问题,没有。

The output of the compiler will be the same regardless of how you've optimised it (unless it's buggy, of course). 无论你如何优化它,编译器的输出都是相同的(当然,除非它有问题)。

In other words, even if you totally stuffed up your compiler flags when compiling gcc , to the point where your particular compiled version of gcc takes a week and a half to compile "Hello World", the actual "Hello World" executable should be identical to the one produced by the "lowest common denominator" gcc (if you use the same flags). 换句话说,即使你在编译gcc时完全填满你的编译器标志,到你的特定编译版本的gcc需要一个半星期来编译“Hello World”,实际的“Hello World”可执行文件应该是相同的到“最低公分母” gcc产生的那个(如果你使用相同的标志)。

(1) It is possible. (1)有可能。 If you introduce a new optimization to your compiler, and re-compile it with this optimization included - it is possible that the re-compiled code will perform better. 如果您为编译器引入了一个新的优化,并使用此优化重新编译它 - 重新编译的代码可能会更好地执行。

(2) No!!!! (2) 没有!!!! A compiler cannot change the logic of the code! 编译器无法改变代码的逻辑! In your case, the logic of the code is the native code produced at the end. 在您的情况下,代码的逻辑是最终生成的本机代码。 So, if compiler A_1 is compiled using compiler A_2 or B, has no affect on the native code produced by A_1 [in here A_1, A_2 are the same compilers, the index is just for clarity]. 因此,如果使用编译器A_2或B编译编译器A_1,则不会影响A_1生成的本机代码[在此处A_1,A_2是相同的编译器,索引仅为了清晰起见]。

a.Well, you can compile the compiler to your system, and maybe it will run faster. a。好的,你可以将编译器编译到你的系统,也许它运行得更快。 like any program. 喜欢任何节目。 (I think that usualy it's not worth it, but do whatever you want). (我认为通常它不值得,但做任何你想做的事)。

b. No. Even if you compile the compiler in your computer, it's behavior should not change, and so the code that it generates also doesn't change. 不。即使您在计算机中编译编译器,它的行为也不应该改变,因此它生成的代码也不会改变。

Will compiling a compiler like GCC result in better performance? 编译像GCC这样的编译器会产生更好的性能吗?

A program compiled specifically to the target platform it is used on will usually perform better than a program compiled for a generic platform. 专门针对其使用的目标平台编译的程序通常比为通用平台编译的程序执行得更好。 Why is this? 为什么是这样? Knowledge about the harware can help the compiler align data to be cache friendly and choose an instruction ordering that plays well with a CPUs pipelining. 有关harware的知识可以帮助编译器将数据与高速缓存友好对齐,并选择与CPU流水线配合良好的指令排序。

The most benefit is usally achieved by leveraging specific instruction sets such as SSE (in its various versions). 通过利用SSE(在其各种版本中)等特定指令集,可以实现最大的好处。

On the other hand, you should ask yourself if a programm like GCC is really CPU bound (much more likely it will be IO bound) and tuning its CPU performance provides any measurable benefit. 另一方面,您应该问自己,像GCC这样的程序是否真的受CPU限制(更可能是IO绑定),调整其CPU性能可以带来任何可衡量的好处。

Will the code compiled by my own compiled compiler perform better 我自己编译的编译器编译的代码是否会表现更好

Hopefully not! 希望不是! Allowing a compiler to optimize a program should never change its behavior. 允许编译器优化程序不应该改变它的行为。 No matter how you compiled your GCC, it should compile code to the same binaries as a generic binary distribution of GCC would. 无论你如何编译你的GCC,它都应该将代码编译为与GCC的通用二进制分发相同的二进制文件。

If code compiled to the specific platform is faster than code compil for a generic platform, why dont we all ship code instead of binaries? 如果编译到特定平台的代码比通用平台的代码编译更快,为什么我们都不发送代码而不是二进制代码? Guess what, some linux distros actually follow this phillosophy, such as Gentoo. 猜猜是什么,一些Linux发行版实际上遵循这个哲学,如Gentoo。 And while you're at it, make sure to built statically linked binaries, disk space is so cheap nowadays and it gives you at least another 0.001% of performance. 当你在它的时候,确保构建静态链接的二进制文件,磁盘空间现在如此便宜,它至少可以提供0.001%的性能。

Alright, that was a bit sarcastic. 好吧,那有点讽刺。 The reason people distribute generic binaries is pretty obvious: It's geneirc, the lowest common denominator and it will work everywhere. 人们分发通用二进制文件的原因非常明显:它是geneirc,最低的共同点,它可以在任何地方使用。 Thats a big bonus in terms of flexibility and user friendlyness. 这在灵活性和用户友好性方面是一个很大的好处。 I remember once compiling Gnome for my Gentoo box, it took a day or two! 我记得曾经为我的Gentoo盒子编译Gnome,花了一两天! (But it must have been so much faster ;-) ) (但它一定要快得多;-))

On the other hand, there are occassions where you want to get the best performance possible and it makes sense to build and optimize for specific architctures. 另一方面,有些情况下您希望获得最佳性能,并且为特定的结构构建和优化是有意义的。

GCC uses a three step bootstraping when building from source. 从源代码构建时,GCC使用三步引导。 Basically it compiles the source three times to ensure build tools and compiler is build successfully. 基本上,它会三次编译源代码,以确保构建工具和编译器成功构建。 This bootstraping is used for validation purpose. 该引导用于验证目的。 However it is possible to use the stage 1 as a benchmark for optimizing later stages. 但是,可以使用阶段1作为优化后期阶段的基准。 You should build GCC with make profiledbootstrap to use this profile based optimization. 您应该使用make profiledbootstrap构建GCC以使用此基于配置文件的优化。

This profile based build process increases the performance of "GCC", but not the software compiled with it, as other answers point out. 其他答案指出,这种基于配置文件的构建过程提高了“GCC”的性能,但没有提高用它编译的软件的性能。

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

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