使用优化标志编译C.
发布时间:2020-12-16 09:35:16 所属栏目:百科 来源:网络整理
导读:我正在比较两个C文件的两种汇编形式,一种是带有优化标志(-O2),另一种是没有. 我的问题是: 为什么在优化的汇编版本中,编译器将main函数放在所有辅助函数之后,而辅助函数在原始汇编代码中首先出现.这在效率方面意味着什么?有人可以详细说明吗? 这是原始的C
我正在比较两个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 ' ' 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 ],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,"Blackn" ) == 0 ) return 1; if( strcmp( str,"Redn" ) == 0 ) return 1; if( strcmp( str,"Bluen" ) == 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,"-2n" ) == 0 ) return 1; if ( strcmp( str,"+2n" ) == 0 ) return 1; return 0; /* If no match above,answer is not valid */ } 解决方法
为了提高效率,约翰是完全正确的.毕竟在目标文件中编译,函数只是符号表中的条目,顺序无关紧要.
对于您的问题,为什么此订单出现如此: >当没有优化时,所有的调用函数只是引用功能的地址.编译器然后可以轻松地发出目标代码按照它遇到的顺序功能的定义.>优化时,编译器替换(至少可能)所有来电它具有的小功能内联代码的定义.所以一般他不能发出代码当他遇到这个功能但是必须等到他最终看到所有被召唤的宣言功能.因此,排放顺序像线性反转的东西调用图的排序. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |