Python 与 Java 性能(运行时速度)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3044620/
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
Python vs. Java performance (runtime speed)
提问by Bijan
Possible Duplicate:
is python slower than java/C#?
可能的重复:
python 比 java/C# 慢吗?
Ignoring all the characteristics of each languages and focusing SOLELY on speed, which language is better performance-wise?
忽略每种语言的所有特征并只关注速度,哪种语言在性能方面更好?
You'd think this would be a rather simple question to answer, but I haven't found a decent one.
你会认为这是一个相当简单的问题,但我还没有找到一个像样的问题。
I'm aware that some types of operations may be faster with python, and vice-versa, but I cannot find any detailed information on this. Can anyone shed some light on the performance differences?
我知道某些类型的操作使用 python 可能会更快,反之亦然,但我找不到任何关于此的详细信息。任何人都可以阐明性能差异吗?
回答by polygenelubricants
Java is faster than Python. Easily.
Java 比 Python 快。容易地。
Python is favorable for many things; speed isn't necessarily one of them.
Python 对很多事情都有利;速度不一定是其中之一。
References
参考
回答by Oli
Different languages do different things with different levels of efficiency.
不同的语言以不同的效率水平做不同的事情。
The Benchmarks Gamehas a whole load of different programming problems implemented in a lot of different languages.
Benchmarks Game有大量不同的编程问题,用许多不同的语言实现。
回答by Michael Aaron Safyan
There is no good answer as Python and Java are both specifications for which there are many different implementations. For example, CPython, IronPython, Jython, and PyPy are just a handful of Python implementations out there. For Java, there is the HotSpot VM, the Mac OS X Java VM, OpenJRE, etc. Jython generates Java bytecode, and so it would be using more-or-less the same underlying Java. CPython implements quite a handful of things directly in C, so it is very fast, but then again Java VMs also implement many functions in C. You would probably have to measure on a function-by-function basis and across a variety of interpreters and VMs in order to make any reasonable statement.
没有好的答案,因为 Python 和 Java 都是有许多不同实现的规范。例如,CPython、IronPython、Jython 和 PyPy 只是少数 Python 实现。对于 Java,有 HotSpot VM、Mac OS X Java VM、OpenJRE 等。Jython 生成 Java 字节码,因此它或多或少会使用相同的底层 Java。CPython 直接用 C 实现了相当多的东西,所以它非常快,但是 Java VM 也用 C 实现了许多功能。您可能必须在逐个函数的基础上并跨各种解释器和VMs 以便做出任何合理的声明。
回答by Aaron Digulla
If you ignore the characteristics of both languages, how do you define "SPEED"? Which features should be in your benchmark and which do you want to omit?
如果忽略两种语言的特点,你如何定义“速度”?哪些特性应该在你的基准测试中,哪些你想省略?
For example:
例如:
- Does it count when Java executes an empty loop faster than Python?
- Or is Python faster when it notices that the loop body is empty, the loop header has no side effects and it optimizes the whole loop away?
- Or is that "a language characteristic"?
- Do you want to know how many bytecodes each language can execute per second?
- Which ones? Only the fast ones or all of them?
- How do you count the Java VM JIT compiler which turns bytecode into CPU-specific assembler code at runtime?
- Do you include code compilation times (which are extra in Java but always included in Python)?
- Java 执行空循环的速度比 Python 快吗?
- 或者当 Python 注意到循环体为空、循环头没有副作用并且优化了整个循环时,它会更快吗?
- 或者那是“语言特征”?
- 您想知道每种语言每秒可以执行多少字节码吗?
- 哪个?只有快速的还是所有的?
- 您如何计算在运行时将字节码转换为特定于 CPU 的汇编代码的 Java VM JIT 编译器?
- 您是否包括代码编译时间(在 Java 中是额外的,但始终包含在 Python 中)?
Conclusion: Your question has no answer because it isn't defined what you want. Even if you made it more clear, the question will probably become academic since you will measure something that doesn't count in real life. For all of my projects, both Java and Python have always been fast enough. Of course, I would prefer one language over the other for a specific problem in a certain context.
结论:你的问题没有答案,因为它没有定义你想要什么。即使你说得更清楚,这个问题也可能会变得很学术,因为你会衡量一些在现实生活中不重要的东西。对于我的所有项目,Java 和 Python 一直都足够快。当然,对于特定上下文中的特定问题,我更喜欢一种语言而不是另一种语言。