编译器如何优化我们的代码?

时间:2022-07-15 03:15:31

I ran into this question when i was answering another guys question. How do compilers optimize the code? Can keywords like const, ... help? Beside the fact with volatiles and inline functions and how to optimize the code all by your self!

当我回答另一个人的问题时,我遇到了这个问题。编译器如何优化代码? const,...等关键字可以帮忙吗?除了挥发性和内联函数以及如何通过自己优化代码之外!

3 个解决方案

#1


10  

Compilers are free to optimize code so long as they can guarantee the semantics of the code are not changed.

编译器可以*地优化代码,只要它们可以保证代码的语义不会改变。

I would suggestion starting at the Compiler optimization wikipedia page as there are many different kinds of optimization that are performed at many different stages.

我建议从编译器优化*页面开始,因为在许多不同阶段执行了许多不同类型的优化。

As you can see, modern compilers are very 'smart' at optimizing code (compiled C code is often faster than hand-written assembly unless the programmer really knows how to take advantage of all the specific processor instructions and quirks). As others have said, write for clarity first based on a good design.

正如您所看到的,现代编译器在优化代码方面非常“智能”(编译的C代码通常比手写编译快,除非程序员真正知道如何利用所有特定的处理器指令和怪癖)。正如其他人所说,首先基于良好的设计来写清楚。

#2


6  

One very big thing you can do ( beyond what the compiler can do for you ) is to be aware of the cache. Since accessing the memory is really time expensive, the cache tries to help you by storing not only the data you accessed it but the nearby elements as well. This is why foo will run so much faster than bar:

你可以做的一件非常重要的事情(超出编译器可以为你做的事情)就是要知道缓存。由于访问内存非常耗时,因此缓存会尝试不仅存储您访问它的数据,还会存储附近的元素。这就是foo运行速度比bar快得多的原因:

array[ NUM_ROWS ][ NUM_COLS ];

foo() 
{
    int row, col;
    int sum = 0;

    // accesses the elements in the array continuously
    for ( row = 0; row < NUM_ROWS ; row++ ) 
    {
         for ( col = 0; col < NUM_COLS; col++ )
         {
              sum += array[ row ][ col ];
         }
    }
}

bar() 
{
    int row, col;
    int sum = 0;

    // skips from row to row ( big jumps that might miss the cache )
    for ( col = 0; col < NUM_COLS ; col++ ) 
    {
         for ( row = 0; row < NUM_ROWS; row++ )
         {
              sum += array[ row ][ col ];
         }
    }
}

Edit: Another thing to be aware of is repeated string concatenation. Done wrong, this can make code that otherwise seems to run in O( n ) actually be in O( n^2 ) - see an article on Joel on Software

编辑:另一件要注意的事情是重复字符串连接。做错了,这可以使得看起来在O(n)中运行的代码实际上在O(n ^ 2)中 - 请参阅关于Joel on Software的文章

Edit: s/disk/memory/

#3


-4  

The rules of optimization:

优化规则:

  1. Don't do it
  2. 不要这样做

  3. Advanced Users Only: Don't do it yet
  4. 仅限高级用户:尚未执行此操作

Edit: The quote (and other information, useful or not) can be found in the CodingHorror article: Hardware is cheap, programmers are expensive. It would be nice to find the 'origin' of this phrase/quote though.

编辑:引用(和其他信息,有用与否)可以在CodingHorror文章中找到:硬件很便宜,程序员很贵。尽管如此,找到这个短语/引用的“起源”会很高兴。

#1


10  

Compilers are free to optimize code so long as they can guarantee the semantics of the code are not changed.

编译器可以*地优化代码,只要它们可以保证代码的语义不会改变。

I would suggestion starting at the Compiler optimization wikipedia page as there are many different kinds of optimization that are performed at many different stages.

我建议从编译器优化*页面开始,因为在许多不同阶段执行了许多不同类型的优化。

As you can see, modern compilers are very 'smart' at optimizing code (compiled C code is often faster than hand-written assembly unless the programmer really knows how to take advantage of all the specific processor instructions and quirks). As others have said, write for clarity first based on a good design.

正如您所看到的,现代编译器在优化代码方面非常“智能”(编译的C代码通常比手写编译快,除非程序员真正知道如何利用所有特定的处理器指令和怪癖)。正如其他人所说,首先基于良好的设计来写清楚。

#2


6  

One very big thing you can do ( beyond what the compiler can do for you ) is to be aware of the cache. Since accessing the memory is really time expensive, the cache tries to help you by storing not only the data you accessed it but the nearby elements as well. This is why foo will run so much faster than bar:

你可以做的一件非常重要的事情(超出编译器可以为你做的事情)就是要知道缓存。由于访问内存非常耗时,因此缓存会尝试不仅存储您访问它的数据,还会存储附近的元素。这就是foo运行速度比bar快得多的原因:

array[ NUM_ROWS ][ NUM_COLS ];

foo() 
{
    int row, col;
    int sum = 0;

    // accesses the elements in the array continuously
    for ( row = 0; row < NUM_ROWS ; row++ ) 
    {
         for ( col = 0; col < NUM_COLS; col++ )
         {
              sum += array[ row ][ col ];
         }
    }
}

bar() 
{
    int row, col;
    int sum = 0;

    // skips from row to row ( big jumps that might miss the cache )
    for ( col = 0; col < NUM_COLS ; col++ ) 
    {
         for ( row = 0; row < NUM_ROWS; row++ )
         {
              sum += array[ row ][ col ];
         }
    }
}

Edit: Another thing to be aware of is repeated string concatenation. Done wrong, this can make code that otherwise seems to run in O( n ) actually be in O( n^2 ) - see an article on Joel on Software

编辑:另一件要注意的事情是重复字符串连接。做错了,这可以使得看起来在O(n)中运行的代码实际上在O(n ^ 2)中 - 请参阅关于Joel on Software的文章

Edit: s/disk/memory/

#3


-4  

The rules of optimization:

优化规则:

  1. Don't do it
  2. 不要这样做

  3. Advanced Users Only: Don't do it yet
  4. 仅限高级用户:尚未执行此操作

Edit: The quote (and other information, useful or not) can be found in the CodingHorror article: Hardware is cheap, programmers are expensive. It would be nice to find the 'origin' of this phrase/quote though.

编辑:引用(和其他信息,有用与否)可以在CodingHorror文章中找到:硬件很便宜,程序员很贵。尽管如此,找到这个短语/引用的“起源”会很高兴。