It seems that I often hear people criticize certain programming languages because they "have poor performance", or because some other language is "faster" in general (not necessarily for a specific application). However, my experience and education have taught me that anytime you have a performance problem, at least one of the following is probably happening:
似乎我经常听到人们批评某些编程语言,因为他们“表现不佳”,或者因为某些其他语言一般“更快”(不一定针对特定应用)。但是,我的经验和教育告诉我,无论何时遇到性能问题,至少可能发生以下一种情况:
瓶颈不在CPU中,而在某些其他设备中,例如网络或硬盘驱动器。
性能不佳是由您的算法引起的,而不是由您使用的语言引起的。
My general impression is that the speed of a programming language itself is all but irrelevant in the vast majority of cases, with exceptions for serious data processing problems. Even in those cases, I believe you could use a hybrid approach and use a lower-level language only for the CPU-intensive pieces so that you wouldn't lose the benefits of the more abstract language altogether.
我的总体印象是,编程语言本身的速度在绝大多数情况下都是无关紧要的,严重的数据处理问题除外。即使在这些情况下,我相信你可以使用混合方法,并且只为CPU密集型部分使用低级语言,这样你就不会完全失去更抽象语言的好处。
Do you agree? Is programming language speed insignificant most of the time, or do the critics have a right to point out language performance issues?
你同意吗?编程语言的速度在大多数时候都是微不足道的,还是批评者有权指出语言表现问题?
I hope this question isn't too subjective, but it seems to me that there should be a relatively objective answer to this.
我希望这个问题不是太主观,但在我看来应该有一个相对客观的答案。
Performance can be a serious concern in libraries, operating systems, and the like. However, I believe that upwards of 90% of the time raw performance is irrelevant.
在图书馆,操作系统等中,性能可能是一个严重的问题。但是,我认为超过90%的原始性能是无关紧要的。
What is more important in many cases is TIMING. Any garbage collected language is going to have some unpredictability in this regard, which makes them unsuited to embedded and realtime design spaces.
在许多情况下更重要的是TIMING。任何垃圾收集语言在这方面都会有一些不可预测性,这使得它们不适合嵌入式和实时设计空间。
The overlap of GC'd and "slow" languages is considerable, and so you may see a language discounted for speed reasons when the real problem is inconsistent timing.
GC'd和“慢”语言的重叠是相当可观的,因此当真正的问题是不一致的时间时,您可能会看到由于速度原因而打折的语言。
There are some allocation/threading/etc. schemes that allow for garbage collection while also guaranteeing the runtime of parts of the system, such as Realtime Java, though I haven't personally seen it in use anywhere.
有一些分配/线程/等。允许垃圾收集的方案,同时也保证系统部分的运行时,例如Realtime Java,尽管我没有亲眼看到它在任何地方使用过。
Short answer: most of the time the speed of the language is irrelevant (within reason), language choices are made based on familiarity and available libraries.
简答:大多数时候语言的速度是无关紧要的(在合理范围内),语言选择是基于熟悉程度和可用的库。
Amazingly, the performance of a system is a combination of the programming language, the system it's executing on, the operations that system is performing and the external resources (network, disk, slow line printers, etc.) that it relies upon.
令人惊讶的是,系统的性能是编程语言,正在执行的系统,系统正在执行的操作以及它依赖的外部资源(网络,磁盘,慢速打印机等)的组合。
If your system is slow, rather than guessing, test it.
如果您的系统很慢,而不是猜测,请测试它。
If there is any "Rule" in computing, it's "Test your assumptions". Everything else is gross guideline.
如果计算中有任何“规则”,那就是“测试你的假设”。其他一切都是严格的准则。
This is impossible to answer so broadly. It's like asking if big engines are a waste in cars. Well, for some people, yes. For others, not at all. And all sorts in between.
这不可能如此广泛地回答。这就像问大型发动机是否是汽车浪费一样。嗯,对某些人来说,是的。对于其他人,根本不是。介于两者之间。
There are a myriad of factors that come in to play. What is your target environment? End-user deployment or servers? Let's suppose we're talking about web development and coding for a server. RoR is well-known to be (relatively) slow. .NET is pretty fast by comparison. But RoR also has RAD qualities that .NET can't compete with.
有许多因素可以发挥作用。你的目标环境是什么?最终用户部署还是服务器?我们假设我们正在讨论服务器的Web开发和编码。众所周知,RoR(相对)缓慢。相比之下,.NET相当快。但是RoR也具有.NET无法与之竞争的RAD特性。
昨天让您的应用程序正常运行比可扩展性更重要吗?
您的商业模式是在您服务页面的毫秒数或您上市时间的情况下生存还是死亡?
您的TCO和应用程序架构是否支持扩展或扩展?你甚至希望扩大规模吗?
Those are just a tiny handful of the questions an architect has to answer when making platform/language decisions. Does speed matter? Sometimes. If I am planning to write a LoB service that will eventually need to scale to thousands of transactions per second, and it will be deployed in an enterprise environment, I will probably go with .NET. If I have an idea for a Web 2.0 business like selling Twitter teeshirts, I need to capitalize on that idea yesterday and I can know I probably won't get slammed with enough business to bring the site down before preparing for it.
这些只是建筑师在制定平台/语言决策时必须回答的极少数问题。速度重要吗?有时。如果我打算编写一个最终需要扩展到每秒数千个事务的LoB服务,并且它将部署在企业环境中,我可能会使用.NET。如果我对像销售Twitter teeshirts这样的Web 2.0业务有所了解,我需要在昨天利用这个想法,我知道在准备之前我可能不会受到足够的业务打击以使网站崩溃。
This is honestly over-simplifying a very complex issue, but hopefully illustrated the point that it's impossible to simply "say" whether it matters or not.
这实际上过分简化了一个非常复杂的问题,但希望说明一点就是说“说”是否重要是不可能的。
I think it's a good question. To answer it requires having a general framework for thinking about performance, so let me try to provide one. (Some of this is going to sound really obvious, but bear with me.)
我认为这是一个很好的问题。要回答它需要一个考虑性能的一般框架,所以让我试着提供一个。 (其中一些听起来很明显,但请耐心等待。)
To keep things simple, let's just consider the simple case of applications that have a specific job to do, and that start, and then finish, and what you care about is wall-clock time. Let's assume a standard CPU cycle rate, and a mono-processor.
为了简单起见,我们只考虑具有特定工作的应用程序的简单情况,然后开始,然后完成,您关心的是挂钟时间。我们假设一个标准的CPU循环速率和一个单处理器。
The time duration consists of a stream of time-slices (nanoseconds, say). To do that job, there is a minimum amount of time required, and it is usually greater than zero. There is no maximum amount of time required. If a program spends longer than the minimum number of nanoseconds, then some of those nanoseconds are being spent, strictly speaking, unnecessarily (i.e. for poor reasons).
持续时间由时间片流组成(例如纳秒)。要做这项工作,所需的时间最短,通常大于零。没有最长的时间。如果程序花费的时间超过最小纳秒数,则严格来说,这些纳秒中的一些是不必要的(即由于不良原因)。
So, to optimize a program's execution time, it is necessary to find the nanoseconds it is spending that do not have to be spent (i.e. that do not have good reasons) and remove them.
因此,为了优化程序的执行时间,有必要找到不必花费的纳秒(即没有充分理由)并删除它们。
One way to do this is to, if possible, step through the program and keep track at each step of why it is doing that step. If the reason is not good, there is an opportunity for removing steps.
实现此目的的一种方法是,如果可能的话,逐步完成程序并跟踪每个步骤,了解它为什么要执行该步骤。如果原因不好,则有机会删除步骤。
Another way to do this is to select nanoseconds at random from the program's execution, and inquire their reasons. For example, the program counter can tell you what the program is doing, but the call stack can tell you why. In order for the nanosecond to be spent for a good reason, every call instruction on the call stack has to have a good reason. If any instruction on the call stack does not have a good reason, then there is an opportunity to optimize. In fact, the amount of time that instruction is on the call stack is the amount of time that would be saved by its removal.
另一种方法是从程序执行中随机选择纳秒,并查询其原因。例如,程序计数器可以告诉您程序正在做什么,但调用堆栈可以告诉您原因。为了有充分理由花费纳秒,调用堆栈上的每个调用指令都有充分的理由。如果调用堆栈上的任何指令没有充分的理由,那么就有机会进行优化。事实上,指令在调用堆栈上的时间量是删除它所节省的时间。
In some kinds of software that are highly asynchronous, message-driven, or interpreted, the call stack may not provide enough information. In that case, to answer why a given nanosecond is being spent may be more difficult. It may require examining more state information than just the call stack. For example, in an interpreter, the stack of the program being interpreted may also need to be examined. However, often the hardware call stack does provide sufficient information, so it is a useful thing to examine.
在某些高度异步,消息驱动或解释的软件中,调用堆栈可能无法提供足够的信息。在这种情况下,回答为什么给定纳秒被花费可能会更困难。它可能需要检查比调用堆栈更多的状态信息。例如,在解释器中,还可能需要检查被解释的程序的堆栈。但是,通常硬件调用堆栈确实提供了足够的信息,因此检查它是一件有用的事情。
Now, to try to answer your question.
现在,试着回答你的问题。
There is such a thing as a "hot spot". This is a small set of addresses that are often at the bottom of the call stack. Nanoseconds spent in that code may or may not have good reasons.
有一个“热点”之类的东西。这是一小组地址,通常位于调用堆栈的底部。在该代码中花费的纳秒可能有也可能没有充分的理由。
There is such a thing as a "performance problem". This is an instruction that often accounts for why nanoseconds are being spent, but that does not have a good reason. Such an instruction may be in a hot spot. It may also be a subroutine call instruction. (It cannot be both.) It may be an instruction to send a message to be processed later, that does not have a good reason for being spent. To optimize software, such instructions (not functions) are what are being looked for.
存在“性能问题”这样的问题。这是一个经常说明为什么要花费纳秒的指令,但这没有充分的理由。这样的指令可能处于热点。它也可以是子程序调用指令。 (它不能同时存在。)它可能是一个发送消息以便稍后处理的指令,这种指令没有充分的理由被用掉。为了优化软件,正在寻找这些指令(而不是功能)。
Languages, loosely speaking, are either compiled into machine language or interpreted. Interpreted languages are usually 1 or 2 orders of magnitude slower than compiled, because they are constantly re-determining what they need to do. However, roughly speaking, this is only a performance problem if it occurs in a hot spot. If a program spends all its time calling compiled library functions, or waiting for I/O completions, then its speed of execution probably doesn't matter, because most of the nanoseconds are being spent for other reasons.
松散地说,语言要么被编译成机器语言,要么被解释。解释语言通常比编译语言慢1或2个数量级,因为它们不断地重新确定他们需要做什么。但是,粗略地说,如果它出现在热点中,这只是一个性能问题。如果程序花费所有时间来调用编译库函数,或者等待I / O完成,那么它的执行速度可能无关紧要,因为大多数纳秒都是由于其他原因而花费的。
Now, certainly, any language or program can in principle be highly non-optimal, but in terms of compilers, for hotspot code, they are mostly pretty good, give or take maybe 30%. If there is a background process involved, like garbage collection, that adds an overhead, but it depends on the rate at which the program generates garbage.
现在,当然,任何语言或程序原则上都可能是高度非最佳的,但就编译器而言,对于热点代码而言,它们大多数都非常好,可以给出或者占用30%。如果涉及后台进程,如垃圾收集,则会增加开销,但这取决于程序生成垃圾的速率。
So to sum up, the speed of a language matters in hotspot code, but not much elsewhere. When a program has been optimized by removal of all other performance problems, and if the hotspot code is actually seen by the compiler/interpreter, then speed of language matters.
总而言之,语言的速度在热点代码中很重要,但在其他地方并不多。通过删除所有其他性能问题来优化程序,并且如果编译器/解释器实际看到热点代码,则语言速度很重要。
Your question is framed very broadly, so I'll try to give a somewhat narrower answer:
你的问题框架非常广泛,所以我会尝试给出一个稍微狭窄的答案:
Unless there is some good reason not to do so, the language for a project should always be chosen from among those languages that will help the project team be productive and produce reliable software that can easily be adapted for future needs. The tradeoffs generally favor high-level languages with automatic memory management.
除非有充分的理由不这样做,否则应始终从能够帮助项目团队提高工作效率并生成可以轻松适应未来需求的可靠软件的语言中选择项目的语言。权衡通常倾向于使用自动内存管理的高级语言。
N.B. There are plenty of good reasons to make other choices, such as compatibility with current products and libraries.
注:做出其他选择有很多充分的理由,例如与当前产品和库的兼容性。
It sometimes happens that when a program is too slow, the quickest and easiest way to speed it up is to rewrite the program (or a critical part) in a new language. This happens most often when the implementation language is interpreted and the new language is compiled.
有时候,当程序太慢时,加快速度的最快捷最简单的方法就是用新语言重写程序(或关键部分)。这种情况最常发生在解释实现语言并编译新语言时。
Example: I got about a 4x speedup out of the OSBF-Lua spam filter by rewriting the lexical analysis of the mail headers. By rewriting from Lua to C I not only went from interpreted to compiled but was able to eliminate an array-bounds check for every input character.
示例:通过重写邮件头的词法分析,我从OSBF-Lua垃圾邮件过滤器中获得了大约4倍的加速。通过从Lua重写为C,我不仅从解释到编译,而且能够消除每个输入字符的数组边界检查。
To answer your question as stated, it is not very often that language performance per se is an issue.
要回答上述问题,语言表现本身并不常见。