原文
https://users.ece.utexas.edu/~adnan/pike.html
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”.
翻譯
Rob Pike的編程五原則
規(guī)則1. 你無法確定程序將花費時間的具體位置。瓶頸往往出現(xiàn)在意想不到的地方,因此不要試圖預測并在證明瓶頸位置之前進行速度優(yōu)化。
規(guī)則2. 測量。在測量之前不要為了速度進行調優(yōu),即使在測量之后,也不要調優(yōu),除非代碼的某一部分壓倒了其他部分。
規(guī)則3. 在n很小時,復雜算法速度很慢,而且n通常很小。復雜算法具有很大的常數(shù)。除非你知道n經(jīng)常會變大,否則不要使用復雜算法。(即使n變大,也要先遵循規(guī)則2。)
規(guī)則4. 復雜算法比簡單算法更容易出錯,而且實現(xiàn)起來更困難。使用簡單算法以及簡單數(shù)據(jù)結構。
規(guī)則5. 數(shù)據(jù)至關重要。如果你選擇了正確的數(shù)據(jù)結構并組織得當,算法幾乎總是不言自明的。數(shù)據(jù)結構而不是算法是編程的核心。
Pike的第1和第2條規(guī)則重新闡述了Tony Hoare的著名格言“過早優(yōu)化是萬惡之源”。Ken Thompson將Pike的第3和第4條規(guī)則重新表述為“懷疑時,使用暴力法”。規(guī)則3和規(guī)則4是KISS設計哲學的實例。規(guī)則5以前由Fred Brooks在《人月神話》中提出。規(guī)則5通常縮寫為“編寫使用智能對象的愚蠢代碼”。
筆記
規(guī)則5倒是沒想到的,但我不是很認同。
反例如Dijkstra最短路徑算法,或者經(jīng)典算法都是反例。
似乎沒有經(jīng)典數(shù)據(jù)結構這個說法。文章來源:http://www.zghlxwxcb.cn/news/detail-829739.html
個人一直覺得是相輔相成的,都很重要,很難分先后。
當然,以數(shù)據(jù)為中心,大部分事務實際都是基礎算法的組合,將數(shù)據(jù)描述清楚了,問題都已經(jīng)解決80%了。
從這個角度(以數(shù)據(jù)為中心)來講,數(shù)據(jù)結構的確優(yōu)先于算法。文章來源地址http://www.zghlxwxcb.cn/news/detail-829739.html
到了這里,關于五個編程原則:Rob Pike‘s 5 Rules of Programming的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!