why is python slow?

Christopher Browne cbbrowne at acm.org
Thu Mar 7 23:54:47 EST 2002


les_ander at yahoo.com (les ander) wrote:
> i am just curious as to why different programming languages have
> different speed. I understand that if a language has an extensive
> grammar (such as c++) the compiler would take longer. But what about
> execution?  why is a perl program faster than similar python
> program? For example when java came out it use to be pretty slow,
> but now it is pretty fast. How was this speed-up acheived and why is
> it not possible to have such a speed up for python?

The complexity of the language grammar will certainly affect how large
the compiler is; the impact of that on speed of execution of compiled
code should be minimal (except to the degree that it's harder to write
good optimizers for complex languages than for simpler languages).

You'd often be able to expect programs deployed in languages that
compile to machine code like C, C++, Ada, and such, to be faster than
programs deployed using environments that use interpreters and/or
bytecode compilers.

The bit of C code
for (i=0; i < 50000; i++) {
  c = c + 1.0;
}

is likely to execute a whole lot faster (assuming it doesn't get
compiled out of existence into a mere "c = 50000.0;") than the Perl:

for ($i=0; $i < 50000; $i++) {
  $c += 1.0;
}

or the Python:

for i in range(50000):
   c = c + 1.0

The latter two will at best involve interpretation of bytecompiled
code, which is a whopping lot slower than the machine language
generated by the C compiler.

Java is an interesting case; the development of "JIT" compilers takes
bytecompiled code and generates machine code which can be of
comparable speed to C/C++/..., and that represents a relatively recent
improvement to Java environments as they have matured.

Pre-JIT, Java would have pretty similar performance characteristics to
Perl and Python.

In all cases, evaluating performance is rather a "black art."  It is
_not_ safe to say that C will "always be faster," or that Python will
"always be slower;" even if that were always true, raw speed is by no
meands the only evaluation criterion, which anyone that has looked at
benchmark comparisons will know.

The sorts of JIT speedups seen with Java don't happen with Python
because Python doesn't directly define a virtual machine that is
amenable to JIT analysis.  But JIT is fairly abtruse, and part of the
point is that Java _without_ JIT "sucks wind badly," performance-wise.
They _needed_ JIT for Java to NOT suck badly.

The answers for performance tuning of Perl and Python will differ from
that.
-- 
(concatenate 'string "cbbrowne" "@canada.com")
http://www3.sympatico.ca/cbbrowne/python.html
Your latest program has been judged UNTASTEFUL by the T daemon;
and automatically deleted.



More information about the Python-list mailing list