Python 比 Java/C# 慢吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/672857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:47:50  来源:igfitidea点击:

Is Python slower than Java/C#?

c#javapythonperformance

提问by yesraaj

Is Python slower than Java/C#?

Python 比 Java/C# 慢吗?

performance-comparison-c-java-python-ruby-jython-jruby-groovy

性能比较-c-java-python-ruby-jython-jruby-groovy

Here is a project that optimizes CPython: unladen-swallow

这里有一个优化 CPython 的项目:unladen-swallow

采纳答案by S.Lott

Don't conflate Language and Run-Time.

不要将语言和运行时混为一谈。

Python (the language) has many run-time implementations.

Python(该语言)有许多运行时实现。

  • CPython is usually interpreted, and will be slower than native-code C#. It might be slower than Java, depending on the Java JIT compiler.

  • JYthon is interpreted in the JVM and has the same performance profile as Java.

  • IronPython relies on the same .NET libraries and IL as C#, so the performance difference will be relatively small.

  • Python can be translated to native code via PyREX, PyToC, and others. In this case, it will generally perform as well as C++. You can -- to an extent -- further optimize C++ and perhaps squeeze out a little bit better performance than unoptimized output from PyREX.

    For more information, see http://arcriley.blogspot.com/2009/03/so-long-pyrex.html

  • CPython 通常是解释型的,并且会比本机代码 C# 慢。它可能比 Java 慢,具体取决于 Java JIT 编译器。

  • JYthon 在 JVM 中被解释并且具有与 Java 相同的性能配置文件。

  • IronPython 依赖于与 C# 相同的 .NET 库和 IL,因此性能差异会相对较小。

  • Python 可以通过 PyREX、PyToC 等转换为本地代码。在这种情况下,它的性能通常与 C++ 一样好。您可以——在某种程度上——进一步优化 C++,或许可以从 PyREX 的未优化输出中挤出更好的性能。

    有关更多信息,请参阅http://arcriley.blogspot.com/2009/03/so-long-pyrex.html

Note that Python (the language) is not slow. Some Python run-times (CPython, for example) will be slower than native-code C++.

请注意,Python(语言)并不慢。某些 Python 运行时(例如 CPython)会比本机代码 C++ 慢。

回答by Jon Skeet

As suggested in comments, you should really provide a test case to reason about. Reasons behind performance differences will change depending on the test being executed.

正如评论中所建议的,您应该真正提供一个测试用例来推理。性能差异背后的原因将根据正在执行的测试而变化。

However, I'd suggest that the static vs dynamic nature may well have a lot to do with it. For non-virtual calls, the JIT-compiled C#/Java is extremely cheap as it can be determined accurately at JIT-time. Even virtual calls just involve a single level of redirection. When binding becomes dynamic, there's a wider range of things to consider.

但是,我建议静态与动态的性质可能与它有很大关系。对于非虚拟调用,JIT 编译的 C#/Java 非常便宜,因为它可以在 JIT 时间准确确定。甚至虚拟调用也只涉及单一级别的重定向。当绑定变得动态时,需要考虑更广泛的事情。

I don't know enough details about Python to claim to understand its exact runtime behaviour, which I suspect may vary with version and implementation too. There is such a thing as "python byte code" which is then executed by a virtual machine - whether this virtual machine actually performs JIT-compilation or not is another matter.

我对 Python 的了解不够详细,无法声称了解其确切的运行时行为,我怀疑这也可能因版本和实现而异。有一种叫做“python 字节码”的东西,然后由虚拟机执行——这个虚拟机是否真的执行 JIT 编译是另一回事。

回答by Micha? Niklas

I think opposite. I can do simple program in Python faster than in Java, and those Python scripts work really fast.

我认为相反。我可以用 Python 比用 Java 更快地完成简单的程序,而且这些 Python 脚本的运行速度非常快。

Of course your question without examples is hard to answer. Maybe you have found slow library, bug etc. Give us more details please.

当然,你的问题没有例子是很难回答的。也许您发现了缓慢的库、错误等。请给我们更多详细信息。

回答by Tom Hawtin - tackline

It is not really correct to ask why Python is slower than Java/C#. How fast is Java? Well, naive interpreters are around ten times slower than optimised compilers. I believe there is a Java bytcode interpreter written in JavaScript - that probably isn't very fast. So, the intended question appears to be "Why is the CPython language system slower than the equivalent Sun, IBM and Oracle JRE and Microsoft .NET runtime?"

问为什么 Python 比 Java/C# 慢是不正确的。Java 有多快?好吧,朴素的解释器比优化的编译器慢十倍左右。我相信有一个用 JavaScript 编写的 Java 字节码解释器 - 这可能不是很快。因此,预期的问题似乎是“为什么 CPython 语言系统比等效的 Sun、IBM 和 Oracle JRE 以及 Microsoft .NET 运行时慢?”

I believe the correct answer is non-technical. The fastest Java and .NET runtime are faster because they have large full time technical teams developing them in performance-competitive environment.

我相信正确的答案是非技术性的。最快的 Java 和 .NET 运行时速度更快,因为它们有大量的全职技术团队在性能竞争环境中开发它们。

Dynamic language systems are easy to implement. Any idiot can do it. I have. Static language systems are more complex to design and implement. A simple static system will tend to run much faster than the equivalent just-working dynamic equivalent. However, it is possible for highly optimised dynamic systems to run almost as fast. I understand some Smalltalk implementation were quite good. An often quoted example of a developed dynamic system is the MIT Lisp Machine.

动态语言系统很容易实现。任何白痴都能做到。我有。静态语言系统的设计和实现更加复杂。一个简单的静态系统往往比等效的刚工作的动态系统运行得快得多。然而,高度优化的动态系统有可能运行得几乎一样快。我知道一些 Smalltalk 实现非常好。一个经常被引用的开发动态系统的例子是MIT Lisp Machine

In addition if the real grunt is being done by library code, then the language system may not matter. Alternatively, the language may encourage (or give time(!)) to develop more efficient algorithms which can easily wipe out constant factor performance differences.

此外,如果真正的咕噜声是由库代码完成的,那么语言系统可能无关紧要。或者,该语言可能会鼓励(或给予时间(!))开发更有效的算法,这些算法可以轻松消除常数因子性能差异。

回答by vartec

What you got there is clear example of writing Java in Python:

你得到的是在 Python 中编写 Java 的清晰示例:

 def __init__(self,size):  
     self.first = None  
     last = None  
     for i in range(size):  
         current = Person(i)  
         if self.first == None : self.first = current  
         if last != None :  
             last.next = current  
             current.prev = last  
         last = current  
     self.first.prev = last  
     last.next = self.first  

A bit more pythonic:

多一点pythonic:

 def __init__(self,size):  
     chain = [Person(i) for i in range(size)]
     self.first = chain[0]
     chain = zip(chain, chain[1:].append(chain[0]))
     for p,n in chain:
        p.next = n
        n.prev = p

回答by Dhananjay Nene

It boils down to the fact that the compilation phase has lesser information to work with and hence the runtime needs to do more work in case of duck typed (dynamically typed) languages.

归结为编译阶段需要处理的信息较少,因此在鸭子类型(动态类型)语言的情况下,运行时需要做更多的工作。

Thus if I am making the method invocation foo.bar(), in case of Java or C++ the invocation to bar can be optimized in the compilation process by discovering the type of "foo" and then directly invoking the method at the memory location where the compiler knows it will be found. Since a python or any other dynamically typed language compiler does not know what type the object foo belongs to, it has to do a type check at runtime and then look up the address of the bar method and then invoke it.

因此,如果我正在调用 foo.bar() 方法,在 Java 或 C++ 的情况下,可以通过发现“foo”的类型然后直接在内存位置调用该方法,从而在编译过程中优化对 bar 的调用编译器知道它会被找到。由于 python 或任何其他动态类型语言编译器不知道对象 foo 属于什么类型,因此它必须在运行时进行类型检查,然后查找 bar 方法的地址,然后调用它。

There are other difficulties a python compiler writer struggles with as well, though the one above hopefully adequately gives an indication. So even with the best compiler writers, statically typed languages are likely to perform much better at runtime.

python 编译器编写者还面临其他困难,尽管上面的问题希望能够充分说明问题。因此,即使有最好的编译器编写者,静态类型语言在运行时也可能表现得更好。

Where dynamically typed languages score are typically in the development time. Due to fewer lines of code to write and maintain, and no compile wait times for developers, the development often goes through much faster.

动态类型语言得分通常在开发阶段。由于需要编写和维护的代码行更少,并且开发人员没有编译等待时间,因此开发过程通常会更快。

回答by DNS

I would argue that the ease and simplicity of writing Python code makes it possible to write more complex code; for example, code that takes advantage of multi-core processors. Since per-core performance has been mostly stagnant for the past 5-10 years, I don't think it's clear that Python programs (whether they're running on CPython or something else) are slower in the long run.

我认为编写 Python 代码的轻松性和简单性使得编写更复杂的代码成为可能;例如,利用多核处理器的代码。由于在过去的 5 到 10 年中,每核性能几乎停滞不前,因此我认为 Python 程序(无论它们是在 CPython 上还是其他东西上运行)从长远来看会变慢并不清楚。

回答by Marko

It doesn't have anything to do with the languages themselves, it's just the fact that java implementationand runtime system(JVM) are very high quality, and that lots of resources have been invested in stability, scalability and performance improvements over the years.

它与语言本身没有任何关系,只是 Java实现运行时系统(JVM) 的质量非常高,并且多年来在稳定性、可扩展性和性能改进方面投入了大量资源。

Contrast that to the fact that CPython implementation just recently implemented eg threaded dispatch in its interpreter which gave it performance boost of up to 20% for certain problems. It's not a good thing as it sounds, it is bad because that kind of basic optimization should be there from the day one.

相比之下,CPython 实现最近才在其解释器中实现了例如线程分派,这使其在某些问题上的性能提升了 20%。这听起来不是一件好事,而是一件坏事,因为从一开始就应该进行这种基本的优化。

回答by Jason Baker

I think it's ultimately that Python doesn't go as far as it can with optimizations. Most of the optimization techniques that are common are for static languages. There areoptimization techniques for dynamic languages, but the modern ones don't seem to make as much use of them as they could. Steve Yegge has an excellent blog post on the subject.

我认为最终是 Python 在优化方面没有达到它所能达到的程度。大多数常见的优化技术都是针对静态语言的。还有对动态语言的优化技术,但似乎是现代的不使尽可能多的使用它们,因为它们可能。Steve Yegge 有一篇关于这个主题的优秀博客文章

EDIT: I just wanted to point out that I'm not necessarily stating this to be critical of Python. I prefer simplicity over unnecessary speed any day.

编辑:我只是想指出,我不一定要说这是对 Python 的批评。我更喜欢简单而不是不必要的速度。

回答by Saleh Al-Zaid

Since it's interpreted and not compiled.. it should be slower in execution time.

由于它被解释而不是编译......它的执行时间应该更慢。

As a table mentioned in Code Complete (second edition)book, page 600,

作为Code Complete(第二版)书第 600 页中提到的表格,

C# equals C++ in execution time(1:1). And Python is slower above hundred times than C++ in execution time(>100:1).

C# 在执行时间上等于 C++ (1:1)。并且 Python 在执行时间上比 C++ 慢一百倍以上(>100:1)。

And Java is slower than C++ by one time and a half (1.5:1).

Java 比 C++ 慢一倍半(1.5:1)。

These statistics are on average. I don't know who made this study, but seems interesting.

这些统计数据是平均的。我不知道是谁做的这项研究,但看起来很有趣。