企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 利用代数恒等式 ``` not a and not b not (a or b) ``` 使用第二种表达式,可以避免一次not 操作,提高性能 ## 削弱运算强度 用加法代替乘法。 用乘法代替幂乘。 利用三角恒等式代换等价的三角函数。 用1ong或int来代替1ong1ong整数(但请注意使用机器字长的整数和非 机器字长整数所带来的差异)。 用定点数或整型数代替浮点数。 用单精度数代替双精度数。 **用移位操作代替整数乘2或除2** bad ``` value= coefficient(0) For power 1 To order value= value coefficient( power )*x^power Next ``` good:把幂改为乘法 ``` value= coefficient(0) pwoerOfX = x For power 1 To order value= value coefficient( power )*x^power pwoerOfX = pwoerOfX * X Next ``` very good:再次减少乘法 ``` value = 0 For power = order to 1Step -1 value = (value+confficient(pwoer)) *x Next value = value + coefficient(0) ``` ## 编译初始化 如把log 计算转为整数 bad ``` unsigned int Log2(unsigned int x){ return (unsigned int) (log(x) / log(2)); } ``` good ``` const double LOG2 = 0.6314718; unsigned int Log2(unsigned int x){ return (unsigned int) (log(x) / LOG2); } ```