Python/Scripting language performance

Curtis Jensen cjensen at bioeng.ucsd.edu
Fri May 31 11:07:34 EDT 2002


Skip Montanaro wrote:
>     Curtis> I'm looking for an explicit explination of why Scripting
>     Curtis> languages, specificaly Python, are slow compared to compiled
>     Curtis> languages.
> 
> Languages like Python and Perl (I think you do both a disservice by calling
> them "scripting" languages) are compiled to fairly high-level virtual
> machines.  The bytecode is then interpreted at run-time.  Interpreting
> bytecode is much less efficient than executing native machine code on real
> hardware.  Compiling dynamically types languages to efficient machine code
> is difficult.  C, C++ and Java are easier to compile to fairly efficient
> machine code because they are statically typed.  Dynamic typing means you
> can't know ahead of time that (for example) the variable named "x" will
> always be an int.  C knows that "int x;" means that it can generate a very
> efficient sequence of machine instructions to perform the expression "x +
> 5".  The Python compiler doesn't know what "x + 5" means because it doesn't
> know, a priori, what the type of x is and thus what the semantics of the
> addition operator are going to be.
> 
> That's not the end of the story, however.  Take a look at the Psyco project
> for a system that provides some tantalizing performance improvements:
> 
>     http://sourceforge.net/projects/psyco
> 
> I believe the Parrot project is also working on a more efficient virtual
> machine for Perl (and eventually other languages).
> 


Thanks for the responce.  Though, I still have some questions.

If Python and Perl are not scripting languages, then what are they?

Also, is dynamic typing the only reason for python's performance hit? 
Does this mean that if I use something like Numeric arrays (from the 
Numeric Module), which pretty much staticaly types it's variables, and 
define these variables at the top of a function or in the constructor of 
a class, that I would avoid the performance hit of dynamic typing?  Are 
there other performance penalties in Python other than dynamic typeing? 
  What are they?

I'm still lost on the point of the byte code.  What's the point?  If 
it's still line by line interpreted, why not just skip the byte code 
compilation and line by line interpret the source?  Is there some 
optimization in the byte code?

I looked at psyco.  From the documentation it looks like it does code 
prediction; both on variable values and also on conditional branches. 
  No matter what the language, better prediction will always yeild a 
performance speed up.  Also, it looks as if psyco has a staticaly typed 
(32-bit) untyped typed. :)  (one type for doubles ints, long ints, 
etc.).  I assume this is an attempt to work around the dynamicaly typed 
variable performance hit.  From this I am again left wondering if 
dynamic typing is the main  reason for the performance hit.  Ineficient 
pre-processing (byte code) would be the other main performance problem.

Thanks for any further comments.

-- 
Curtis Jensen
cjensen at bioeng.ucsd.edu
http://www-bioeng.ucsd.edu/~cjensen/
FAX (425) 740-1451




More information about the Python-list mailing list