编码的作业,使编译器/优化器做出更快的程序 [英] Coding Practices which enable the compiler/optimizer to make a faster program

查看:115
本文介绍了编码的作业,使编译器/优化器做出更快的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多年前,C编译器并不特别聪明。作为一种变通方法K&安培; R发明了注册关键字,暗示编译器,也许这将是一个好主意,让这个变量在内部寄存器。他们还提出了三级运营商,以帮助更好地产生code。

Many years ago, C compilers were not particularly smart. As a workaround K&R invented the register keyword, to hint to the compiler, that maybe it would be a good idea to keep this variable in an internal register. They also made the tertiary operator to help generate better code.

随着时间的推移,编译器成熟。他们成了他们的流量分析,让他们做出什么值寄存器持有比你可能做的更好的决定很聪明。寄存器关键字变得不重要。

As time passed, the compilers matured. They became very smart in that their flow analysis allowing them to make better decisions about what values to hold in registers than you could possibly do. The register keyword became unimportant.

FORTRAN可以为某些种类的操作是比C快,由于别名的问题。在仔细编码理论,人们可以围绕该限制,以使优化器生成速度更快的code。

FORTRAN can be faster than C for some sorts of operations, due to alias issues. In theory with careful coding, one can get around this restriction to enable the optimizer to generate faster code.

什么编码实践提供可能使编译器/优化器生成速度更快code?


  • 确定您使用的平台和编译器,将AP preciated。

  • 为什么技术似乎工作?

  • 样品code是鼓励。

下面是一个<一个href=\"http://stackoverflow.com/questions/1130290/c-coding-practices-for-performance-or-$c$c-size-beyond-what-a-compiler-does\">related问题

这个问题是不是整个过程来分析,优化。假设该程序已经被正确写入,以饱满的优化编译,测试并投入生产。有可能在你的code构造,从做最好的工作,它可以禁止优化。你能做些什么重构,将删除这些禁令,并允许优化器生成速度更快code?

This question is not about the overall process to profile, and optimize. Assume that the program has been written correctly, compiled with full optimization, tested and put into production. There may be constructs in your code that prohibit the optimizer from doing the best job that it can. What can you do to refactor that will remove these prohibitions, and allow the optimizer to generate even faster code?

偏移相关链接

推荐答案

写入局部变量,而不是输出参数!这对于周围走样怠工得到一个巨大的帮助。例如,如果你的code看起来像

Write to local variables and not output arguments! This can be a huge help for getting around aliasing slowdowns. For example, if your code looks like

void DoSomething(const Foo& foo1, const Foo* foo2, int numFoo, Foo& barOut)
{
    for (int i=0; i<numFoo, i++)
    {
         barOut.munge(foo1, foo2[i]);
    }
}

编译器不知道foo1!= barOut,因而具有通过循环foo1每次重装。它也无法读取foo2的[I]直到barOut写完成。你可以开始瞎搞受限的指针,但它只是作为有效的(和更清晰),要做到这一点:

the compiler doesn't know that foo1 != barOut, and thus has to reload foo1 each time through the loop. It also can't read foo2[i] until the write to barOut is finished. You could start messing around with restricted pointers, but it's just as effective (and much clearer) to do this:

void DoSomethingFaster(const Foo& foo1, const Foo* foo2, int numFoo, Foo& barOut)
{
    Foo barTemp = barOut;
    for (int i=0; i<numFoo, i++)
    {
         barTemp.munge(foo1, foo2[i]);
    }
    barOut = barTemp;
}

这听起来很傻,但是编译器可以更聪明处理局部变量,因为它不可能在内存中的任何参数重叠。这可以帮助您避免可怕的负荷命中存储(由弗朗西斯·博伊文在这个线程提及)。

It sounds silly, but the compiler can be much smarter dealing with the local variable, since it can't possibly overlap in memory with any of the arguments. This can help you avoid the dreaded load-hit-store (mentioned by Francis Boivin in this thread).

这篇关于编码的作业,使编译器/优化器做出更快的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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