[Tutor] How it is better than java

Steven D'Aprano steve at pearwood.info
Tue Sep 20 02:27:12 CEST 2011

Ashish Gaonker wrote:
> My obvious thinking is : Java being compiled language , must be faster then
> a interpreted   language.

There are three misunderstandings with that statement.


Languages are neither "compiled" or "interpreted". Languages are syntax 
and grammar. Implementations are either compiled, or interpreted, or 
both: for example, there are C interpreters and C compilers. And the 
quality of both can vary significantly.

Asking which language is faster is like asking which is faster, Ford or 
Toyota? That depends on the particular model, and the conditions, and 
the skill of the driver.

It is ironic that you contrast Java as "compiled" and Python as 
"interpreted", because that is *marketing*. When Java first came out, 
Sun didn't want people describing it as "interpreted", which it was, so 
they popularized the term "byte-code compiler" just so that they could 
talk about Java being compiled. Java would compile your Java source code 
to byte-code which was then interpreted by a virtual machine.

That is *exactly* what Python does: it compiles Python source code to 
byte-code which is interpreted by a virtual machine, just like Java. 
What do you think the .pyc files contain, and what the compile() 
function does? And yet, even back in the 1980s, everybody swallowed 
Sun's marketing and called Java a compiled language and Python an 
interpreted language. This is a testament to Sun spending millions in 

Byte-code compilation is a lot older than Java. Pascal used something 
similar in the early 1970s, called a p-machine. Even Apple's Hypertalk 
did the same thing, only they called it "tokenized" code instead of 
compiled. Java's big innovation was to convince people to use the term 
"compiler" for what was functionally identical to an interpreter.

Of course, Sun (now owned by Oracle) put in a lot of money into Java. 
Millions. Today, Java does have implementations which compile source 
code to machine code. But there are Python implementations that do the 
same, such as Nuitka and Compyler. (I don't know how successful or good 
they are.)

Secondly, what do you mean by "faster"? Faster to write? Faster to 
compile? Faster to run? Faster for the engine to start up? Even today, 
after Sun has spent tens of millions on Java development, the Java 
Runtime Environment is a big, heavyweight machine that takes a long time 
to start up: cold starts can easily take 30 seconds. That makes Java 
completely unsuitable for small, lightweight tasks: in the time it takes 
for a *single* Java program just to start up, you could possibly run a 
dozen Python programs or a hundred interpreted bash scripts.

But again, that's an *implementation*, not a hard rule about Java. There 
is at least one third-party JRE which claims to have startup times twice 
as fast as the Sun/Oracle JRE.

Either way, once you take startup time into account, sometimes Python 
scripts are not only faster to write and faster to maintain, but faster 
to run as well.

Thirdly, there is no rule of nature that a compiled program to do a job 
must be faster than an interpreted program to do the same thing. This 
depends entirely on the quality of implementation of both: a poor 
compiler may easily generate bad, slow code that takes longer to run 
than a wickedly fast and efficient interpreter. E.g. a compiled version 
of bubblesort will still be slower than an interpreted version of quicksort.

Nothing drives this home more than PyPy, a Just In Time optimizing 
version of Python. PyPy uses a JIT compiler to run code sometimes FASTER 
than the equivalent program in optimized C.

Yes. Faster than C. You read that right.


Of course, benchmarks are notoriously flawed, especially micro-
benchmarks. What they *really* answer is not "which language is faster?" 
(a nonsense question, as I have tried to explain) but "which 
implementation is faster with these particular settings on this 
particular task?", a much more practical question.

As exciting as it is to see Python code run faster than C code, it 
shouldn't really surprise anyone that a JIT dynamic compiler with cross 
module optimization beats a static compiler without it. What *is* 
surprising is that a small group of open-source developers have been 
able to build an optimizing JIT compiler for Python of such quality.

Or at least, it is surprising to people who think that quality code can 
only come from big proprietary companies with huge budgets.

What's really exciting though is that now PyPy can be fast enough for 
large programs that traditionally needed to be written in C can now be 
(sometimes!) written in Python:


Who cares whether Java, or C, is "faster" than Python? The important 
question should be, is Python fast enough for the job I have to do?

> Can you share some more especially as compared to Java / .net (two primarily
> used languages in enterprise language & web based applications)

You can look at Jython (Python for the JRE) and IronPython (Python for 
.Net). There is also an older implementation, Python For .Net, which 
runs the standard CPython implementation on .Net, but I don't think it 
is still maintained -- IronPython has taken over its niche. There's also 
JPype, which claims to give full access to Java libraries in Python.


More information about the Tutor mailing list