在剖析代码时,我应该使用匹配(gcc)编译器优化标志吗? [英] Should I use matching (gcc) compiler optimization flags when profiling the code?

查看:128
本文介绍了在剖析代码时,我应该使用匹配(gcc)编译器优化标志吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编译代码时使用 -O3 ,现在我需要配置文件它。对于性能分析,我有两个主要选择: valgrind --tool = callgrind gprof



Valgrind(callgrind)docs状态:


希望使用调试信息(-g选项)和优化打开。


但是,在 Agner Fog的C ++优化手册,我已阅读以下内容:


许多优化选项与调试不兼容。调试器可以一次执行一行
代码,并显示所有变量的值。显然,这是不可能的
当代码的部分已重新排序,内联或优化远离。通常
使程序可执行文件的两个版本:具有完全调试支持的调试版本
,在程序开发期间使用,并且具有所有相关
优化选项的发布版本被打开。大多数IDE(集成开发环境)有
设施,用于制作调试版本和目标文件和可执行文件的发布版本。
确保区分这两个版本,并关闭调试和分析支持
可执行文件的优化版本。


这似乎与 callgrind 指令info标志 -g 。如果我以下列方式启用调试:

  -ggdb -DFULLDEBUG 
pre>

我不会导致此选项与 -O3 优化标志冲突吗?使用这两个选项一起对我没有意义,我到目前为止的阅读。



如果我使用say -O3 优化标志,我可以通过使用以下方式编译代码以及其他分析信息: / p>

  -pg 


$ b b

仍然使用valgrind配置文件?



使用

编译的代码是否有意义有用

  -ggdb -DFULLDEBUG -O0 

标志?它似乎愚蠢 - 不内联函数和展开循环可能会改变代码中的瓶颈,所以这应该只用于开发,以获得代码实际上正确做东西。



用一个优化标志编译代码是否有意义,以及用另一个优化标志来编译代码?

解决方案

为什么要剖析?



你应该只分析优化代码的常见的智慧是基于假设代码几乎是最佳的开始,如果有是显着的加速,它不是。



你应该把加速的发现,如果他们是错误。许多人使用 此方法 这样做。



在删除不必要的计算之后,如果你仍然有严格的CPU循环,即你没有花所有的时间在系统或库或I / O例程中,优化器看不到,然后打开 - O3,让它做它的魔术。


I am using -O3 when compiling the code, and now I need to profile it. For profiling, there are two main choices I came accross: valgrind --tool=callgrind and gprof.

Valgrind (callgrind) docs state:

As with Cachegrind, you probably want to compile with debugging info (the -g option) and with optimization turned on.

However, in the C++ optimization book by Agner Fog, I have read the following:

Many optimization options are incompatible with debugging. A debugger can execute a code one line at a time and show the values of all variables. Obviously, this is not possible when parts of the code have been reordered, inlined, or optimized away. It is common to make two versions of a program executable: a debug version with full debugging support which is used during program development, and a release version with all relevant optimization options turned on. Most IDE's (Integrated Development Environments) have facilities for making a debug version and a release version of object files and executables. Make sure to distinguish these two versions and turn off debugging and profiling support in the optimized version of the executable.

This seems to conflict the callgrind instructions to compile the code with the debugging info flag -g. If I enable debugging in the following way:

-ggdb -DFULLDEBUG

am I not causing this option to conflict with the -O3 optimization flag? Using those two options together makes no sense to me after what I have read so far.

If I use say -O3 optimization flag, can I compile the code with additional profiling info by using:

-pg

and still profile it with valgrind?

Does it ever make sense to profile a code compiled with

-ggdb -DFULLDEBUG -O0

flags? It seems silly - not inlining functions and unrolling loops may shift the bottlenecks in the code, so this should be used for development only, to get the code to actually do stuff properly.

Does it ever make sense to compile the code with one optimization flag, and profile the code compiled with another optimization flag?

解决方案

Why are you profiling? Just to get measurements or to find speedups?

The common wisdom that you should only profile optimized code is based on assuming the code is nearly optimal to begin with, which if there are significant speedups, it is not.

You should treat the finding of speedups as if they were bugs. Many people use this method of doing so.

After you've removed needless computations, if you still have tight CPU loops, i.e. you're not spending all your time in system or library or I/O routines the optimizer doesn't see, then turn on -O3, and let it do its magic.

这篇关于在剖析代码时,我应该使用匹配(gcc)编译器优化标志吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆