使用正确的算法和数据结构

Editor’s Note: Speed and performance is an increasingly important piece of the success of any piece of software and the developer who wrote it. In this piece from 97 Things Every Programmer Should Know, JC van Winkel — who has taught UNIX and programming languages since 1992 and now shares his wisdom as a member of Google’s Site Reliability Engineering team, and founding member and lead educator of the SRE education team, SRE EDU — shares how to avoid novice programming errors and allow your user to better appreciate your programming speed by choosing the correct algorithms and data structures.We’d love to know your thoughts on this piece.

编者注: 速度和性能已成为任何软件和编写该软件的开发人员成功的重要因素。 JC van Winkel 在这本《 每个程序员应该知道的97件事》一 书中,他自1992年以来一直教授UNIX和编程语言,现在他作为Google Site Reliability Engineering团队的一员以及SRE教育团队的创始成员和首席教育者分享他的智慧。 ,SRE EDU —分享了如何避免新手编程错误,以及如何通过选择正确的算法和数据结构让您的用户更好地理解您的编程速度。我们很乐意了解您对此的想法。

A big bank with many branch offices complained that the new computers it had bought for the tellers were too slow. This was in the time before everyone used electronic banking, and ATMs were not as widespread as they are now. People would visit the bank far more often, and the slow computers were making the people queue up. Consequently, the bank threatened to break its contract with the vendor.

一家设有许多分支机构的大银行抱怨说,它为出纳员购买的新计算机太慢。 那时是每个人都使用电子银行的时代,而自动柜员机并不像现在这样普及。 人们会更频繁地访问银行,而慢速的计算机使人们排队。 因此,银行威胁要中断与卖方的合同。

The vendor sent a performance analysis and tuning specialist to determine the cause of the delays. He soon found one specific program running on the terminal that consumed almost all the CPU capacity. Using a profiling tool, he zoomed in on the program and he could see the function that was the culprit. The source code read:

供应商派出了性能分析和调整专家来确定延迟的原因。 他很快发现终端上运行了一个特定的程序,该程序几乎消耗了所有CPU容量。 使用剖析工具,他放大了程序,并且可以看到是罪魁祸首的功能。 源代码为:

for (i=0; i<strlen(s); ++i) {

对于(i = 0; i <strlen(s); ++ i){

if (… s[i] …) …

如果(…s [i]…)…

}

}

And string s was, on average, thousands of characters long. The code (written by the bank) was quickly changed, and the bank tellers lived happily ever after.…

平均而言,字符串s的长度为数千个字符。 代码(由银行编写)很快就被更改,从此以后,银行出纳员过着幸福的生活。…

SHOULDN’T THE PROGRAMMER have done better than to use code that needlessly scaled quadratically?

程序员应该做得比使用不必要地按平方缩放的代码还要好吗?

Each call to strlen traversed every one of the many thousand characters in the string to find its terminating null character. The string, however, never changed. By determining its length in advance, the programmer could have saved thousands of calls to strlen (and millions of loop executions):

每次对strlen的调用都遍历字符串中的数千个字符中的每一个,以找到其终止的空字符。 但是,该字符串从未更改。 通过提前确定其长度,程序员可以保存成千上万的strlen调用(以及数百万次循环执行):

n=strlen(s);

n = strlen(s);

for (i=0; i<n; ++i) {

对于(i = 0; i <n; ++ i){

if (… s[i] …) …

如果(…s [i]…)…

}

}

Everyone knows the adage “first make it work, then make it work fast” to avoid the pitfalls of micro-optimization. But the preceding example would almost make you believe that the programmer followed the Machiavellian adagio “first make it work slowly.”

每个人都知道这句格言“首先使其工作,然后使其快速工作”,以避免微优化的弊端。 但是前面的示例几乎使您相信程序员遵循了Machiavellian的“自动调速器”。

This thoughtlessness is something you may come across more than once. And it is not just a “don’t reinvent the wheel” thing. Sometimes novice programmers just start typing away without really thinking, and suddenly they have “invented” bubble sort. They may even be bragging about it.

您可能不只一次遇到这种无意识的事情。 这不仅是“不要重新发明轮子”的事情。 有时,新手程序员会开始打字而没有真正思考,突然之间,他们“发明了”冒泡排序。 他们甚至可能在吹牛。

The other side of choosing the right algorithm is the choice of data structure. It can make a big difference: using a linked list for a collection of a million items you want to search through — compared to a hashed data structure or a binary tree — will have a big impact on the user’s appreciation of your programming.

选择正确算法的另一面是数据结构的选择。 它可以产生很大的变化:与散列数据结构或二叉树相比,使用链表收集要搜索的一百万个项目的集合将对用户对您的编程的欣赏产生很大的影响。

Programmers should not reinvent the wheel, and should use existing libraries where possible. But to be able to avoid problems like the bank’s, they should also be educated about algorithms and how they scale. Is it just the eye candy in modern text editors that makes them as slow as old-school programs like WordStar in the 1980s? Many say reuse in programming is paramount. Above all, however, programmers should know when, what, and how to reuse. To do that, they should have knowledge of the problem domain and of algorithms and data structures.

程序员不应重蹈覆辙,应尽可能使用现有的库。 但是,为了避免出现类似银行的问题,还应该对算法以及它们的扩展方式进行教育。 仅仅是现代文本编辑器中令人眼花candy乱的东西使它们像1980年代的WordStar这样的老式程序慢吗? 许多人说编程中的重用至关重要。 但是,最重要的是,程序员应该知道何时,什么以及如何重用。 为此,他们应该了解问题域以及算法和数据结构。

A good programmer should also know when to use an abominable algorithm. For example, if the problem domain dictates that there can never be more than five items (like the number of dice in a Yahtzee game), you know that you always have to sort at most five items. In that case, bubble sort might actually be the most efficient way to sort the items. Every dog has its day.

一个好的程序员还应该知道何时使用可恶的算法。 例如,如果问题域指示最多只能有五个项目(例如Yahtzee游戏中的骰子数量),则您必须始终最多对五个项目进行排序。 在这种情况下,冒泡排序实际上可能是对项目进行排序的最有效方法。 每只狗都有自己的一天。

So, read some good books — and make sure you understand them. And if you really read Donald Knuth’s The Art of Computer Programming (Addison-Wesley Professional), well, you might even be lucky: find a mistake by the author, and you’ll earn one of Don Knuth’s hexadecimal dollar ($2.56) checks.

因此,请阅读一些好书-确保您理解它们。 而且,如果您真的读过Donald Knuth的《计算机编程艺术》 (Addison-Wesley Professional),您甚至可能会很幸运:发现作者的错误,您将获得Don Knuth的十六进制美元($ 2.56)支票之一。

学得更快。 深入挖掘。 看得更远。 (Learn faster. Dig deeper. See farther.)

Join the O’Reilly online learning platform. Get a free trial today and find answers on the fly, or master something new and useful.

加入O'Reilly在线学习平台。 立即获得免费试用版,即时找到答案,或者掌握一些新的有用的知识。

Learn more

学到更多

JC van Winkel is a Lead Educator and Senior Site Reliability Engineer at Google. He is also the Dutch representative for C++ standardization. JC was a board member of the Netherlands Unix User’s group (NLUUG) for 12 years, during 6 of which he also served as chair.

JC van Winkel是Google的首席教育家兼高级站点可靠性工程师。 他还是C ++标准化的荷兰代表。 JC曾是荷兰Unix用户组(NLUUG)的董事会成员12年,在此期间他还担任了6年的主席。

翻译自: https://medium.com/oreillymedia/use-the-right-algorithm-and-data-structure-cfbe97a7209d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值