Just curious about speed of Python and Java.. Intuitively, Python should be much slower than java, but I want to know more...Could anybody give me more? or introduce some nice post to read?
-
1shootout.alioth.debian.org/u32/python.phpOscarRyz– OscarRyz2010-12-30 02:57:35 +00:00Commented Dec 30, 2010 at 2:57
-
2What exactly is your reason for caring?Karl Knechtel– Karl Knechtel2010-12-30 03:02:39 +00:00Commented Dec 30, 2010 at 3:02
-
Speed only matters for the specific application you have in mind. Please post the code you want to compare in Python and in Java. Then, as part of posting the code you want to compare, please post the run times for that code. Then we can tell you which is faster.S.Lott– S.Lott2010-12-30 03:27:22 +00:00Commented Dec 30, 2010 at 3:27
-
3I think it is a fair question to ask for results 'in general' -- one does not always start with specific use case in mind, but wants to know typical cases. Not everyone has same narrow scope of solving one concrete problem at a time; others like learning in advance.StaxMan– StaxMan2010-12-30 08:52:41 +00:00Commented Dec 30, 2010 at 8:52
-
1@StaxMan: Since a bad choice of algorithm can make "in general" comparisons utterly invalid, I'm not sure they're helpful. How would you compare a Python list comprehension against a Java implementation that had to be built from scratch? I don't see how "in general" can be valid when there are so many special cases and features.S.Lott– S.Lott2010-12-30 19:56:07 +00:00Commented Dec 30, 2010 at 19:56
4 Answers
The current standard implementation of Python (CPython) is slower than Java because the standard CPython implementation doesn't have a powerful JIT compiler. Yet.
There have been several projects with the aim of producing a faster implement of Python:
From what I've tried some of these projects can give very good speed ups for specific algorithms, but you still won't get it to run as fast as Java for typical application code. Most of the current effort seems now to be directed towards PyPy.
5 Comments
The lack of a JIT mentioned is one reason, but another reason is that Python is dynamic. Yes, that does make the language slower. You can see for yourself by using Cython.
A function written in Python can often be compiled to C with Cython. It makes it faster. But it get's really fast when you start adding type information to the variables and parameters, as both Cython and the C-compiler can start applying various simple optimizations that you can't do when the types are dynamic.
So one part of the difference is the inherent dynamicism of Python.
On the future: Python 3 has function annotations: http://www.python.org/dev/peps/pep-3107/ I expect that in a couple of years time, the JIT compilers like PyPy and UnladenSwallow will use this information, and you'll see Python being just as fast as Java, and with some careful applying of Cython, even faster. :)
Comments
I do not have data points to give, but one interesting aspect is that there are Python implementations on JVM (ditto for many other dynamic/scripting languages) -- JPython and Jython for example. This could allow some Python applications to run at speeds comparable to native Java applications, assuming implementation of Python runtime itself (on JVM) is efficient.
Comments
There are many great answers here as to why Java is faster than Python, 2 of the most common answers are that Python is dynamically typed, and that Java has several extremely powerful Just-in Time Compilers (2 Production quality, several experimental and not for general use) in it's arsenal, with only the C# Common Language Runtime capable of matching it. This is true, but even so, there is one last reason as to why Java is still faster, and oddly enough, it has to do with differences in how Java's Interpreter is designed, vs Python's Interpreter.
Right now, this is the source code of the Interpreter in production for the OpenJDK/HotSpot JVM, Java's reference implementation (There is actually another legacy Interpreter in the JVM source code which was the old one written by James Gosling himself way back when Java was first created, but that one is outdated and not compiled into the actual binary unless you compile it from source with special flags for debugging purposes. Interestingly enough, this is the interpreter responsible for earning Java the reputation of being horrendously slow back in those days): https://github.com/openjdk/jdk/blob/master/src/hotspot/share/interpreter/templateInterpreter.cpp
This, in contrast, is the code segment of the CPython interpreter that executes Python opcode: https://github.com/python/cpython/blob/master/Python/ceval.c#L1847
Notice something different between the 2?
While CPython has a massive for loop with a switch case for every single opcode possible (This is true for almost every other interpreter out there actually, other than Java), there is not a single loop, if else, or switch case in Java's Interpreter. Why is this?
The answer is that Java's Interpreter is a special type called a Template Interpreter, which to date is the only one of it's kind. Unlike most designs, instead of a switch case to evaluate Java bytecode, Java's Interpreter has a big arraylist of bytecode, mapped to native machine language when the application is started. This way, the Java Interpreter doesn't need to evaluate bytecode at all, it just inserts the bytecode as an array index, loads native machine language, and directly runs it on the CPU. This means Java's "Interpreter" is actually a discount Compiler, since it runs your code directly on the hardware. CPython on the other hand, like many other interpreters today, is a run of the mill bytecode interpreter, which processes python opcode in software. This obviously makes Python slower to run than Java, even without JIT.
As to why Java has such a unique Interpreter design not used anywhere else, it's because it needs to run interpreted code directly with JIT Compiled code seamlessly, and the ingenious design of having the Interpreter contain a table with bytecode->machine language pairs rather that directly execute it in software was the best way to achieve this goal.