C++ 如何在 g++ 中使用配置文件引导优化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4365980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use profile guided optimizations in g++?
提问by nakiya
Also, can anyone point me to a good tutorial on the subject? I can't find any.
另外,任何人都可以向我指出有关该主题的好教程吗?我找不到任何。
采纳答案by Maister
-fprofile-generate will instrument the application with profiling code. The application will, while actually running, log certain events that could improve performance if this usage pattern was known at compile time. Branches, possibility for inlining, etc, can all be logged, but I'm not sure in detail how GCC implements this.
-fprofile-generate 将使用分析代码检测应用程序。如果在编译时已知此使用模式,应用程序将在实际运行时记录某些可以提高性能的事件。分支、内联的可能性等都可以记录,但我不确定 GCC 是如何实现这一点的。
After the program exits, it will dump all this data into *.gcda files, which are essentially log data for a test run. After rebuilding the application with -fprofile-use flag, GCC will take the *.gcda log data into account when doing its optimizations, usually increasing the performance significantly. Of course, this depends on many factors.
程序退出后,它会将所有这些数据转储到 *.gcda 文件中,这些文件本质上是测试运行的日志数据。使用 -fprofile-use 标志重建应用程序后,GCC 在进行优化时会考虑 *.gcda 日志数据,通常会显着提高性能。当然,这取决于很多因素。
回答by rogerdpack
From this example:
从这个例子:
g++ -O3 -fprofile-generate [more params here, like -march=native ...] -o executable_name
// run my program's benchmarks, or something to stress its most common path
g++ -O3 -fprofile-use [more params here, like -march=native...] -o executable_name
Basically, you initially compile and link with this extra flag for both compiling and linking: -fprofile-generate
(from here).
基本上,您最初使用此额外标志进行编译和链接以进行编译和链接:(-fprofile-generate
来自此处)。
Then, when you run it, by default it will create .gcda files "next" to your .o files, it seems (hard coded to the full path where they were built).
然后,当你运行它时,默认情况下它会在你的 .o 文件“旁边”创建 .gcda 文件,看起来(硬编码到它们构建的完整路径)。
You can optionally change where it creates these .gcda files with the -fprofile-dir=XXX setting.
您可以选择使用 -fprofile-dir=XXX设置更改创建这些 .gcda 文件的位置。
Then you re compile and relink using the -fprofile-use
parameter, and it compiles it using profile guided goodness.
然后您使用-fprofile-use
参数重新编译和重新链接,它使用配置文件引导的优点编译它。
回答by Zan Lynx
The tricky bit is setting up the makefiles.
棘手的一点是设置makefile。
You definitely need separate output directories for object files. I would recommend naming them "profile" and "release". You might have to copy the *.gcda files that result from the profile run so that GCC finds them in the release build step.
您肯定需要单独的目标文件输出目录。我建议将它们命名为“配置文件”和“发布”。您可能需要复制配置文件运行产生的 *.gcda 文件,以便 GCC 在发布构建步骤中找到它们。
The result will almost certainly be faster. It will probably be larger as well. The -fprofile-use option enables many other optimization steps that are otherwise only enabled by -O3.
结果几乎肯定会更快。它也可能会更大。-fprofile-use 选项启用许多其他优化步骤,否则只能由 -O3 启用。