简体   繁体   English

是否可以使用gdb或其他工具来检测复杂程序的某些部分(例如循环),这些部分花费比目标优化所需的时间更多的时间?

[英]Can gdb or other tool be used to detect parts of a complex program (e.g. loops) that take more time than expected for targeting optimization?

As the title implies basically: Say we have a complex program and we want to make it faster however we can. 正如标题所暗示的那样:假设我们有一个复杂的程序,但是我们希望可以更快地使它更快。 Can we somehow detect which loops or other parts of its structure take most of the time for targeting them for optimizations? 我们能否以某种方式检测出哪些循环或其结构的其他部分大部分时间用于优化目标?

edit: Notice, of importance is that the software is assumed to be very complex and we can't check each loop or other structure one by one, putting timers in them etc.. 编辑:请注意,重要的是该软件被假定为非常复杂,我们无法一一检查每个循环或其他结构,也不能在其中放置计时器。

You're looking for a profiler. 您正在寻找分析器。 There are several around; 周围有几处; since you mention gcc you might want to check gprof (part of binutils). 因为您提到gcc,所以您可能要检查gprof(binutils的一部分)。 There's also Google Perf Tools although I have never used them. 还有Google Perf Tools,尽管我从未使用过。

You can use GDB for that, by this method . 您可以使用GDB的是, 用这种方法

Here's a blow-by-blow example of using it to optimize a realistically complex program. 这是一个使用它来优化实际复杂程序的引人注目的示例

You may find "hotspots" that you can optimize, but more generally the things that give you the greatest opportunity for saving time are mid-level function calls that you can avoid. 您可能会发现可以优化的“热点”,但更一般而言,为您节省时间最多的事情是可以避免的中级函数调用。

  1. One example is, say, calling a function to extract information from a database, where the function is being called multiple times, when with some extra coding the result from a prior call could be used. 例如,调用一个函数以从数据库中提取信息,该函数被多次调用,而当使用一些额外的编码时,可以使用先前调用的结果。 Often such calls are small and innocent-looking, and you're totally surprised to learn how much they're costing, as an overall percent of time. 通常,此类呼叫的规模很小且看起来很天真,并且您完全惊讶于得知它们花费了多少(占总时间的百分比)。

  2. Another example is doing some low-level I/O that escapes attention, but actually costs a hefty percent of clock time. 另一个例子是做一些低级的I / O,这些I / O引起了人们的注意,但实际上却花费了大量的时钟时间。

  3. Another example is tidal waves of notifications that propagate from seemingly trivial changes to data. 另一个示例是通知的潮流,这些通知从看似微不足道的更改传播到数据。

Another good tool for finding these problems is Zoom . 查找这些问题的另一个很好的工具是Zoom

Here's a discussion of the technical issues , but basically what to look for is: 这里是对技术问题的讨论 ,但是基本上要寻找的是:

  • It should tell you inclusive percent of time, at line-level resolution , not just functions. 它应该告诉您包括 行级分辨率 在内的时间百分比 ,而不仅仅是功能。 a) Only knowing that a function is costly still leaves you wondering where the lines are in it that you should look at. a)仅知道某个函数的成本仍然很高,这使您想知道应该在其中查看哪些行。 b) Inclusive percent tells the true cost of the line - how much bottom-line time it is responsible for and would not be spent if it were not there. b)包含百分比表示生产线的实际成本-它负责多少底线时间,如果不存在,将不花费。

  • It should include both I/O (ie blocked) time and CPU time, not just CPU time. 它应同时包括I / O(即阻塞)时间和CPU时间,而不仅仅是CPU时间。 A tool that only considers CPU time will not see the first two problems mentioned above. 仅考虑CPU时间的工具不会看到上面提到的前两个问题。

  • If your program is interactive, the tool should operate only during the time you care about, and not while waiting for user input. 如果您的程序是交互式程序,则该工具应仅在您关心的时间内运行,而不是在等待用户输入时运行。 You don't want to include head-scratching time in your program's performance statistics. 您不想在程序的性能统计信息中包括抓紧时间。

gprof breaks it down by function. gprof按功能将其分解。 If you have many different loops in one function, it might not tell you which loop is taking the time. 如果一个函数中有许多不同的循环,则可能无法告诉您哪个循环在浪费时间。 This is a clue to refactor ;-) 这是重构的线索;-)

暂无
暂无

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

相关问题 检测是否存在任何其他线程的方法(例如,在 fork 之前) - Ways to detect if any other threads exist (e.g. prior to fork) 在Python中使用Prefix实现的很好的前缀,它涵盖了C程序的更多运算符(例如&lt;,&lt;=等)? - Good Infix to Prefix implementation in Python that covers more operators (e.g. <, <=, etc) of a C program? 复杂的Arduino项目有哪些工具(例如IDE,调试器)? - What tools (e.g. IDEs, Debuggers) are there for complex Arduino projects? 为什么不能将任意表达式用作数组大小,例如int [0,1]? - Why arbitrary expressions can't be used as an array size, e.g. int[0,1]? 在 PTY 程序中,诸如 TAB 和 KEY_UP 之类的键不能按预期工作(例如,KEY_UP 变为“^[[A”]) - Keys like TAB and KEY_UP don't work as expected (e.g. KEY_UP becomes “^[[A”) in PTY program 为什么一个代码示例比其他代码需要更多时间执行? - Why one sample of code take more time to execute than other? %c 和其他符号(例如“°​​”)编译后不显示 - %c and other symbols (e.g. " ° ") not displayed after compiling 基本块的组成部分是什么? (例如分支、目标、入口、出口) - What constitutes the parts of a basic block? (e.g. branches, targets, entry, exit) 检测可移动驱动器(例如USB闪存驱动器)C / C ++ - Detect removable drive (e.g. USB flash drive) C/C++ GDB - 访问复数的实部和虚部 - GDB - Accessing real and imaginary parts of a complex number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM