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


http://www.taodudu.cc/news/show-5963712.html

相关文章:

  • 数据分析里常用的五个统计学概念,你知道几个?
  • 概率论 x 数据分析:从贝叶斯定理到概率分布
  • 高中就开始学的正态分布,原来如此重要!
  • break return continue
  • a++ ++a 自增和自减
  • 正则表达式提取工具regularTools
  • 2015-2016-1 学期《软件工程》学生名单-- PS:教材使用《构建之法》第二版 --邹欣著...
  • springBoot的注解
  • 高帧厅逆势增长引领中国影院疫后复苏 未来观众为“技术”买单
  • 当ChatGPT遇见stable-diffusion,你不敢相信的创意艺术之旅!
  • 图片降噪怎么搞?教你三种解决方法
  • 人“贱”人爱——香港电影“贱”星X档案
  • 互联网大厂2021中秋礼盒battle,把我都看馋了
  • 漫画:当文科生与理科生同台说爱(AI)
  • ChatGPT 的背后:OpenAI 创始人Sam Altman如何用微软的数十亿美元打造了全球最热门技术...
  • 加非猫身世背景
  • 小学英语单词分类汇总
  • 漫话:布隆算法概述
  • “互联网造车”的误会
  • 幼儿园衔接测试卷c语言,2020年实验幼儿园幼升小衔接班期中考试试题C卷 附答案.doc...
  • 霸州市津郎计算机学校,2018-2019年廊坊市霸州市津东小学一年级上册语文模拟月考无答案.doc...
  • 率土表情包【原创】第三弹
  • EditText 禁止输入表情包的正则表达式
  • 使用node爬取表情包存进数据库
  • 如果我要用Python偷表情包
  • TV项目测试
  • 电视TV端使用RecyclerView开发遇到的问题
  • 智能电视TV开发---手机直播视频客户端与智能电视通信
  • 智能电视TV分辨率处理方案--第1问
  • adb wifi连接调试应用--适用于手机、平板、电视TV等

使用正确的算法和数据结构相关推荐

  1. noj数据结构稀疏矩阵的加法十字链表_一个算法毁了一款好游戏?算法和数据结构到底有多重要?...

    来源 | 异步 | 文末赠书 前段时间大火的国产游戏--<太吾绘卷>,由于创新的玩法和精良的制作一度广受好评,然而随着玩家游戏的深入和时长的积累,发现该游戏在玩的过程中游戏外的问题很多很多 ...

  2. 浅谈算法和数据结构: 五 优先级队列与堆排序

    原文:浅谈算法和数据结构: 五 优先级队列与堆排序 在很多应用中,我们通常需要按照优先级情况对待处理对象进行处理,比如首先处理优先级最高的对象,然后处理次高的对象.最简单的一个例子就是,在手机上玩游戏 ...

  3. 利用for循环调用插入方法批量插入 一条失败_算法与数据结构(1):基础部分——以插入排序为例...

    本文将会以插入排序为例,介绍算法与数据结构的基础部分. 插入排序 排序可以说是整个算法中最为基础,最为重要的一部分,而插入排序正是排序算法中最简单的一种解决办法. 什么是排序问题? 输入:n个数的一个 ...

  4. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  5. 算法与数据结构--空间复杂度O(1)遍历树

    大家好~我叫「小鹿鹿鹿」,是本卖萌小屋的第二位签约作(萌)者(货).和小夕一样现在在从事NLP相关工作,希望和大家分享NLP相关的.不限于NLP的各种小想法,新技术.这是我的第一篇试水文章,初来乍到, ...

  6. 算法与数据结构简单启蒙,我当年学习算法走过的坑

    1.碎碎念 我的算法启蒙来自于紫书算法竞赛入门经典,但是不得不说从语言过度到算法,紫书并不是一个很好的开始.当时整本书除了数学和图论其实是看完了的,但真的有印象的大约只有暴力枚举法中枚举排列,子集生成 ...

  7. js 冒泡排序_JS 里的简易算法和数据结构之复杂度

    原文:https://www.freecodecamp.org/news/the-complexity-of-simple-algorithms-and-data-structures-in-java ...

  8. 算法与数据结构学习路线

    随着科学技术的发展,人工智能已经逐渐渗透到各个行业,这是一个相当有前景的专业领域. 其中,算法工程师这一职位更是非常火爆,在急缺大量人才的同时,也吸引了众多求职者,那么,初学者该如何学好算法呢? 算法 ...

  9. 别头疼了,你要的算法和数据结构的学习路线来了!

    随着科学技术的发展,人工智能已经逐渐渗透到各个行业,这是一个相当有前景的专业领域. 其中,算法工程师这一职位更是非常火爆,在急缺大量人才的同时,也吸引了众多求职者,那么,初学者该如何学好算法呢? 算法 ...

最新文章

  1. 求求你别再用offset和limit分页了
  2. 社会科技奖不是新鲜事?如何真正做大
  3. 【Android 进程保活】应用进程拉活 ( 系统 Service 机制拉活 | Service 组件 onStartCommand 方法分析 | 源码资源 )
  4. linux下mysql的数据库简单备份脚本
  5. Ansible: hosts文件拆分为inventory和定义inventory全局变量
  6. Linux Kernel系列 - 黄牛X内核代码凝视
  7. java 实现协方差_无监督数据降维技术-主成分分析(PCA)的Python实现
  8. 18.MongoDB之balancer
  9. HTML常用标签、文本格式化标签:加粗、倾斜、删除线、下划线等
  10. java如何将mp4写入光盘_iOS - 读取/写入mp4视频的XMP元数据
  11. html page 制作,webpageHTML
  12. 递归函数c语言结束条件,满足动态条件时退出递归函数
  13. float类型为什么有6位有效数字
  14. 关于新型能源的一点思考.
  15. JavaWeb-Day01
  16. Lintcode题目总结
  17. 整车研发项目阶段归纳
  18. 鼠标按下一直执行代码,鼠标松开停止代码执行
  19. Sanic学习——初识Sanic
  20. 如何创建html文件

热门文章

  1. blogs rails crud
  2. 决策树_(预剪枝和后剪枝)_以判断西瓜好坏为例
  3. 超好用的前端开源 Markdown编辑器 赶紧收藏起来
  4. 【唤醒屏幕总结】java.lang.RuntimeException: WakeLock under-locked target
  5. 微信小程序搜索框自动补全功能
  6. 【无标题】读孙子兵法,品启强人生
  7. Java Stream 流集合去重排序
  8. CLSRSC-505: The root script cannot proceed on this node ora19c02 because the root script
  9. matlab中snapnow命令,SVM学习笔记(一)
  10. 在uniapp中使用canvas制作海报(二)