简体   繁体   English

使用优化标志编译 C

[英]Compiling C with optimization flag

I am comparing two assembly forms of two C files, one with an optimization flag (-O2), and the other without.我正在比较两个 C 文件的两个程序集 forms,一个带有优化标志 (-O2),另一个没有。

My question is:我的问题是:

Why is that in the optimized assembly version, the compiler puts the main function after all the helper functions, whereas the helper functions come first in the original assembly code.为什么在优化的汇编版本中,编译器将主function放在所有辅助函数之后,而辅助函数在原始汇编代码中排在最前面。 What does that mean in terms of efficiency?这在效率方面意味着什么? Can someone please elaborate?有人可以详细说明吗?

Here is the original C file.这是原始的 C 文件。 Thanks!谢谢!

// Based on a source file found in a beginner's discussion on multidimensional arrays here:
// http://www.dreamincode.net/forums/topic/71617-3d-arrays/

#include <stdio.h>
#include <string.h>

int validColor( char *str );
int mathProblem( char *str );

// 1) Asks from six 'theoretical' different users one of two questions,
// allowing only three answers for each.
// 2) Stores the response into a multidimensional array

int main( void )
{
        char myArray[ 6 ][ 3 ][ 20 ]; /* Six users, three questions, and up to 19 characters per answer
(strings are '\0' terminated), though no "true" answer is that long */
        int i, valid;

/* User instructions */
        for( i = 0; i < 6; i++ )
        {
if ( i % 2 == 0 )
{
valid = 0;
while( valid == 0 )
{
                 printf( "Enter your favorite color : " );

                 fgets( myArray[ i ][ 0 ], 20, stdin ); /* Get answer to first question */

if( myArray[ i ][ 0 ][ 0 ] == '\n' ) /* If the user just presses enter, move to next question */
break; /* Jump out of while loop */

valid = validColor( myArray [i ][ 0 ] ); /* Call function to validate answer */
}
}
if ( i % 2 == 1 )
{
valid = 0;
while( valid == 0)
                        {
                                printf( "The roots of (x - 4)^2 : " );

                                fgets(myArray[ i ][ 0 ], 20, stdin); /* Get answer to second question */

                                if(myArray[ i ][ 0 ][ 0 ] == '\n') /* If the user just presses enter, move to next question */
                                        break; /* Jump out of while loop */

                                valid = mathProblem( myArray[ i ][ 0 ] ); /* Call function to validate answer */
                        }
}
        }

        return 0;
}

int validColor( char *str )
{
if( strcmp( str, "Black\n" ) == 0 )
return 1;
if( strcmp( str, "Red\n" ) == 0 )
return 1;
if( strcmp( str, "Blue\n" ) == 0 )
return 1;

return 0; /* If no match above, answer is not valid */
}

int mathProblem( char *str )
{
if ( atoi( str ) == 2 ) /* Function call for analysis purposes */
return 1;
if ( strcmp( str, "-2\n" ) == 0 )
return 1;
if ( strcmp( str, "+2\n" ) == 0 )
return 1;

return 0; /* If no match above, answer is not valid */
}

For the efficiency, John is completely right.对于效率,约翰是完全正确的。 After all is compiled in an object file, functions are just entries in a symbol table, order doesn't matter.毕竟编译在 object 文件中,函数只是符号表中的条目,顺序无关紧要。

For your question why this order appears so:对于您的问题,为什么此订单会如此显示:

  • when not optimizing, all calls to functions are just references to the address of the function.不优化时,所有对函数的调用都只是对 function 的地址的引用。 The compiler can then easily emit the object code in the order it encounters the definitions of the functions.然后,编译器可以轻松地按照遇到函数定义的顺序发出 object 代码。
  • when optimizing, the compiler replaces (at least potentially) all calls to small functions for which it has the definitions at hand by inline code.在优化时,编译器会(至少可能)替换所有对内联代码定义的小函数的调用。 So generally he can't emit the code when he encounters the function but has to wait until he eventually sees the declaration of all called functions.所以一般情况下,当他遇到 function 时他不能发出代码,但必须等到他最终看到所有被调用函数的声明。 The emission order is thus something like an invers linear ordering of the call graph.因此,发射顺序类似于调用图的逆线性排序。

It will make no real difference in terms of efficiency.它不会在效率方面产生真正的影响。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM