2024-10-27 09:23:50 +00:00

17 lines
987 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

C++代码性能优化的方法
1. 使用const关键字
2. 避免频繁进行内存分配和释放
3. 尽量减少值传递,多用引用来传递参数
4. 局部变量VS静态变量多次调用局部更快因为局部变量可以读入寄存器静态不行
5. ++i和i++引申出的效率问题(某些情况下,使用迭代器可能比使用指针更优:迭代器++i可以减少一次构造和析构
6. 避免使用多重继承
7. 低级代码使用位运算替代算术运算
8. 使用编译器自带的优化选项
9. 将小粒度函数声明为内联函数inline
10. 循环内定义,还是循环外定义对象:由构造函数和复制操作的效率比较决定
11. 小函数放在同一循环内效率高大函数分开放效率更高由cpu寄存器决定
12. 尽量少使用dynamic_cast
13. 减少除法运算的使用,除数尽量使用常量,让编译器有空间优化
14. 多用直接初始化
15. 循环展开加速