使用优化标志编译 C

标签 c gcc assembly optimization

我正在比较两个 C 文件的两种汇编形式,一个带有优化标志 (-O2),另一个没有。

我的问题是:

为什么在优化的汇编版本中,编译器将 main 函数放在所有辅助函数之后,而辅助函数在原始汇编代码中排在第一位。这在效率方面意味着什么?有人可以详细说明吗?

这是原始的 C 文件。谢谢!

// 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 */
}

最佳答案

对于效率,John 是完全正确的。毕竟编译到目标文件中,函数只是符号表中的条目,顺序无关紧要。

关于为什么这个顺序会这样出现的问题:

  • 当不优化时,所有调用 函数只是对 函数的地址。编译器 然后可以轻松发出目标代码 按照它遇到的顺序 函数的定义。
  • 优化时,编译器会替换 (至少可能)所有调用 它具有的小功能 内联代码手头的定义。 所以一般他不能发出代码 当他遇到函数时,但是 必须等到他最终看到 所有人的声明 职能。因此发射顺序是 像逆线性的东西 调用图的排序。

关于使用优化标志编译 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6313275/

相关文章:

使用 MinGW 在 Windows 上编译小型 Gcc 项目

gcc - 关于序言/调用函数gcc intel x86的一些问题

assembly - 如何使用 SIMD 指令转置 16x16 矩阵?

c - 寻求有关安装和使用mips-gcc交叉编译器以生成自定义ASM的帮助

c - x86 - C 使用虚拟地址还是线性地址?

c - 这个集会怎么可能?

c - “execle ” linux和嵌入式linux的区别

c++ - 如何将结果存储到文本文件中?

C++ linux 套接字在单个调用中将字符串数组(char **)作为连接字符串发送

c - GTK-doc 不呈现结构的文档注释