前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则Per.1,2,3,4 慎重地优化代码

C++核心准则Per.1,2,3,4 慎重地优化代码

作者头像
面向对象思考
发布2020-06-24 10:42:50
2910
发布2020-06-24 10:42:50
举报
文章被收录于专栏:C++核心准则原文翻译

Per.1: Don't optimize without reason

Per.1:没有明确理由不要进行优化

Reason(原因)

If there is no need for optimization, the main result of the effort will be more errors and higher maintenance costs.

如果没有优化的需求,这种努力的最大结果就是更多的错误和更高维护成本。

Note(注意)

Some people optimize out of habit or because it's fun.

有些人只是处于习惯或兴趣进行优化。

Per.2: Don't optimize prematurely

Per.2:不要过早优化代码

Reason(原因)

Elaborately optimized code is usually larger and harder to change than unoptimized code.

耗费精力优化的代码通常比未经有优化的代码更大,更难修改。

Per.3: Don't optimize something that's not performance critical

Per.3:不要优化性能要求不高的代码

Reason(原因)

Optimizing a non-performance-critical part of a program has no effect on system performance.

优化性能要求不高的代码对系统性能没有帮助。

Note(注意)

If your program spends most of its time waiting for the web or for a human, optimization of in-memory computation is probably useless.

Put another way: If your program spends 4% of its processing time doing computation A and 40% of its time doing computation B, a 50% improvement on A is only as impactful as a 5% improvement on B. (If you don't even know how much time is spent on A or B, see Per.1 and Per.2.)

如果你的程序花费大量时间拥堵等待网页或者用户输入,优化内存内的计算性能很可能不会有什么效果。采用另外的路线:如果你的程序如果你的程序花费4%的CPU时间由于计算A,而40%的CPU时间用用户计算B,将A的性能提高50%其效果只相当于B的性能提高5%。(如果你甚至不知道A和B消耗多少CPU时间,参见Per.1和Per.2)

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#per1-dont-optimize-without-reason

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-06-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Per.1: Don't optimize without reason
  • Reason(原因)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档