Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about Speed: Python VS Java

Tags:

java

python

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?

like image 953
Josh Morrison Avatar asked Dec 30 '10 02:12

Josh Morrison


2 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:

  • Psyco
  • Unladen Swallow
  • PyPy

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.

like image 173
Mark Byers Avatar answered Oct 04 '22 01:10

Mark Byers


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. :)

like image 22
Lennart Regebro Avatar answered Oct 03 '22 23:10

Lennart Regebro