罗布·派克的编程规则 (1989)
Rob Pike’s Rules of Programming (1989)

原始链接: https://users.ece.utexas.edu/~adnan/pike.html

程序员在编写软件时应遵循五个基本准则: 1)在开发过程中忽略有关性能的假设并关注功能。 不要试图预测哪些领域可能会导致稍后出现速度减慢或错误,而应仅在测试表明问题存在后才测量和解决问题。 这就是所谓的先测量后优化的原则。 2) 避免在项目的早期阶段过度陷入优化,尤其是在涉及较小的数据集时。 即使是复杂的算法在处理小输入大小时也往往表现不佳,因为它们复杂的计算会导致巨大的开销成本。 在尝试提高速度或效率之前,请确保这些努力实际上根据真实数据趋势产生了有意义的结果。 3) 在大多数情况下,由于实施时间较短、错误较少且随着时间的推移更易于维护,因此更简单的解决方案将胜过更复杂的替代方案。 默认情况下,选择简单的技术,而不是投入额外的资源来研究新技术。 4) 然而,这条一般规则也有例外——有时根据给定上下文中的特定要求或约束,需要更先进的方法。 尽管如此,仍应使用成本效益分析框架仔细评估此类场景,同时考虑技术挑战和运营费用。 5)主要关注选择适当类型的数据结构来处理常见操作,因为无论其他地方采用的复杂程度如何,这些选择都会严重影响整体算法行为。 利用智能对象设计的更简单的方法通常会提供更简单的途径来满足用户需求,而不会过度复杂化应用程序架构或功能。 这种方法也称为“KISS”哲学(“保持简单、愚蠢”),具有许多优点,包括由于计算复杂性降低而提高执行速度、通过最小化错误传播机会提高可靠性以及简化不同组件之间的接口,从而提高可靠性。 来自共同的基本原则。 因此,它还使开发人员能够更快速、更高效地工作,同时减少与长期故障排除和调试现有系统相关的持续支持成本。

根据本文材料中的讨论,您对性能优化与遵循编程原则有何建议或建议?
相关文章

原文

Rob Pike's 5 Rules of Programming

  • Rule 1. You can't tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you've proven that's where the bottleneck is.
  • Rule 2. Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest.
  • Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don't get fancy. (Even if n does get big, use Rule 2 first.)
  • Rule 4. Fancy algorithms are buggier than simple ones, and they're much harder to implement. Use simple algorithms as well as simple data structures.
  • Rule 5. Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.

Pike's rules 1 and 2 restate Tony Hoare's famous maxim "Premature optimization is the root of all evil." Ken Thompson rephrased Pike's rules 3 and 4 as "When in doubt, use brute force.". Rules 3 and 4 are instances of the design philosophy KISS. Rule 5 was previously stated by Fred Brooks in The Mythical Man-Month. Rule 5 is often shortened to "write stupid code that uses smart objects".

联系我们 contact @ memedata.com