为回路吊装仍然是C code进行有效的人工优化? [英] Is Loop Hoisting still a valid manual optimization for C code?

查看:111
本文介绍了为回路吊装仍然是C code进行有效的人工优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用最新的gcc编译器,我还是要想想这些类型的手动循环优化,或将编译器照顾他们对我不够好?


解决方案

如果你的分析器告诉你有一个循环的问题,也只有这样,需要注意的一件事是在循环内存引用其中的的知道的是整个循环不变,但编译器不会。下面是一个人为的例子,鼓泡元素到数组的末尾:

 的(; I< A->长度 -  1;我++)
    swap_elements(A,I,I + 1);

的可能知道,调用 swap_elements 不改变 A-&GT的价值;长度,但如果 swap_elements 的定义是在另一个源文件中,它很可能是,编译器不会。因此,它可能是值得的吊装 A->中计算;长度圈外的:

  INT N = A->的长度;
对于(; I< N - 1;我++)
    swap_elements(A,I,I + 1);

在性能关键的内部循环,我的学生获得可衡量的速度提升有像这样的转变。<​​/ P>

请注意,有没有必要葫芦 N-1 的计算;任何优化的编译器是完全能够局部变量之间发现循环不变量计算。它的内存引用和函数调用可能会比较困难。而code。与 N-1 更明显正确的。

正如其他人所指出的那样,你有没有生意做任何的操作,直到您已经成型,并已发现循环,实际上是相当重要的性能瓶颈。

Using the latest gcc compiler, do I still have to think about these types of manual loop optimizations, or will the compiler take care of them for me well enough?

解决方案

If your profiler tells you there is a problem with a loop, and only then, a thing to watch out for is a memory reference in the loop which you know is invariant across the loop but the compiler does not. Here's a contrived example, bubbling an element out to the end of an array:

for ( ; i < a->length - 1; i++)
    swap_elements(a, i, i+1);

You may know that the call to swap_elements does not change the value of a->length, but if the definition of swap_elements is in another source file, it is quite likely that the compiler does not. Hence it can be worthwhile hoisting the computation of a->length out of the loop:

int n = a->length;
for ( ; i < n - 1; i++)
    swap_elements(a, i, i+1);

On performance-critical inner loops, my students get measurable speedups with transformations like this one.

Note that there's no need to hoist the computation of n-1; any optimizing compiler is perfectly capable of discovering loop-invariant computations among local variables. It's memory references and function calls that may be more difficult. And the code with n-1 is more manifestly correct.

As others have noted, you have no business doing any of this until you've profiled and have discovered that the loop is a performance bottleneck that actually matters.

这篇关于为回路吊装仍然是C code进行有效的人工优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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