Kai-Uwe BuxSounds bogus to me. Here are two routines:typedef unsigned char u_char;inlineu_char sum ( u_char a, u_char b ) {if ( a == 0 && b == 0 ) return 0;if ( a == 0 && b == 1 ) return 1;if ( a == 0 && b == 2 ) return 2;if ( a == 0 && b == 3 ) return 3;if ( a == 0 && b == 4 ) return 4;if ( a == 0 && b == 5 ) return 5;if ( a == 0 && b == 6 ) return 6;if ( a == 0 && b == 7 ) return 7;if ( a == 1 && b == 0 ) return 1;if ( a == 1 && b == 1 ) return 2;if ( a == 1 && b == 2 ) return 3;if ( a == 1 && b == 3 ) return 4;if ( a == 1 && b == 4 ) return 5;if ( a == 1 && b == 5 ) return 6;if ( a == 1 && b == 6 ) return 7;if ( a == 1 && b == 7 ) return 8;if ( a == 2 && b == 0 ) return 2;if ( a == 2 && b == 1 ) return 3;if ( a == 2 && b == 2 ) return 4;if ( a == 2 && b == 3 ) return 5;if ( a == 2 && b == 4 ) return 6;if ( a == 2 && b == 5 ) return 7;if ( a == 2 && b == 6 ) return 8;if ( a == 2 && b == 7 ) return 9;if ( a == 3 && b == 0 ) return 3;if ( a == 3 && b == 1 ) return 4;if ( a == 3 && b == 2 ) return 5;if ( a == 3 && b == 3 ) return 6;if ( a == 3 && b == 4 ) return 7;if ( a == 3 && b == 5 ) return 8;if ( a == 3 && b == 6 ) return 9;if ( a == 3 && b == 7 ) return 10;if ( a == 4 && b == 0 ) return 4;if ( a == 4 && b == 1 ) return 5;if ( a == 4 && b == 2 ) return 6;if ( a == 4 && b == 3 ) return 7;if ( a == 4 && b == 4 ) return 8;if ( a == 4 && b == 5 ) return 9 ;if ( a == 4 && b == 6 ) return 10;if ( a == 4 && b == 7 ) return 11;return 0;}inlineu_char sum1 ( u_char a, u_char b ) {return( a + b );}Would you expect sum1 to be faster? You are right that it does not add,however it performs quite a few tests, which also cost time.BestKai-Uwe Bux Corey是什么让你认为评论的行比添加的更快?运算符? 据我所知,假设没有使用编译器优化,每个 注释行可以转换成两个减法(SUB)操作 (整数比较是减法和检查零),逻辑AND 操作,和一个非常慢(与加法和减法相比)跳转 (JMP)操作,然后是一个任务( MOV)操作。到目前为止,你有x和y,(x == 0)的结果,(y == 577)的结果,(x == 0&& y = 577)的结果, 所有操作数都不太可能存储在寄存器中以便快速访问。 一个简单的添加只是一个添加(ADD)操作加上一个赋值 操作。并且因为只有x,y和x + y的结果被存储和读取( 结果是x + y自然地在ACC中),很可能是整个操作 在寄存器上完成,这意味着非常快。 通过编译器优化,编译器可能会看到x 和y的定义,然后分配给x和y,所以 迭代范围内的x和y的值是完全可预测的(x = 577和y = 1154),它是不会无论如何都要添加。 最后,未注释的代码比您设计的代码更明确,更简单,更易读。 问候, BenCorey, what makes you think the commented lines are faster than the addtionoperator?As far as I know, and assuming no compiler optimization is used, eachcommented line can be translated into two subtraction (SUB) operations(comparison on integers is substraction and check zero), a logical ANDoperation, and a very slow (as compared to addtion and subtraction) jump(JMP) operation, followed by an assignment (MOV) operation. So far you havex and y, result of (x==0), result of (y==577), result of (x==0 && y==577),it is unlikely that all operands are stored in register for fast access.A simple addtion is just an addtion (ADD) operation plus an assignmentoperation. And because only x, y and result of x+y are stored and read (theresult of x+y naturally in the ACC), it is likely that the whole operationis done on registers, which means very fast.With compiler optimization, the compiler probably sees the definition of xand y followed by assignment to x and y, so the values of x and y within theiteration scope are entirely predictable (x = 577 and y = 1154), that itwouldn''t bother to add anyway.Finally, the uncommented code is much more explicit, simple, readable thanthe one you devised.Regards,Ben 这篇关于闪电计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 09:55