9

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?

32
  • 1
    shootout.alioth.debian.org/u32/python.php Commented Dec 30, 2010 at 2:57
  • 2
    What exactly is your reason for caring? Commented 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. Commented Dec 30, 2010 at 3:27
  • 3
    I 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. Commented 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. Commented Dec 30, 2010 at 19:56

4 Answers 4

14

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Or rather, a universal JITer. It has Psyco, which only runs on IA32 systems.
There are implementations of python that include a JIT compiler. psyco.sourceforge.net and codespeak.net/pypy/dist/pypy/doc come to mind.
Be careful of conflating Python -- the language -- with a specific implementation of Python. A Py2Exe implementation might be considerably faster than a CPython implementation.
recently released PyPy 1.5 is much faster than CPython, in 6 of the benchmarks on speed.pypy.org it is more than 10x faster. It would be great to see some latest PyPy vs Java speed comparison.
PyPy 3.11 simply refuses to run some simple valid Python code (that PyPy 3.10 and CPython 3.13 can run). Until PyPy is fully compatible with the official Python language, it cannot be really considered an alternative.
11

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

2

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

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.