Is python really slow?

Kragen Sitaker kragen at pobox.com
Wed May 22 03:02:55 EDT 2002


Chris <chrisl_ak at hotmail.com> writes:
> I spend a fair amount of time on perl newsgroups, having the need to use 
> perl for various parts of my job. I am now learning Python. I am a bit 
> disheartened at how common it seems to code only some of a program in 
> Python and use C for other parts, and how much discussion here involves 
> using C for X and Y. I don't have time to learn another language. I don't 
> see nearly as much discussion about doing this in the Perl newgroups.

I've spent a fair amount of time on Perl newsgroups, too, and writing
stuff in Perl.  Here are my hypotheses on the causes of this difference:

1. Python is really slow, because it's highly dynamic and because it's
interpreted.  This is true of Perl, too.  Primitive operations in both
languages are roughly 100 to 1000 times slower than in languages
compiled to machine code.  Perl is usually marginally faster than
Python, although there are some cases I've seen where it's drastically
faster.  Both languages also use bucketloads of memory.

2. Perl programmers have a lot of basic problems with things like
failing to open files, leaving variables uninitialized, and
accidentally clobbering global variables that are inadvertently shared
between different parts of a program.  Python catches these errors and
tells you about them, so you don't need to post to a newsgroup to find
out what went wrong.  Fewer basic questions means the advanced
questions constitute a greater portion of the traffic.

3. Perl is extremely popular among novice programmers and
nonprogrammers (compared, anyway, to other programming languages.)
These people do not typically try to extend Perl (or Python) with C,
because it's a lot harder than writing stuff in Perl (or Python).

4. Perl is a pain in the butt to extend in C.  Python is easy to
extend in C.  So more people extend Python in C, but that still
requires writing C, which is comparatively more difficult than Python
to debug.

People usually write C extensions to either Python or Perl for one of
three reasons:
- their Perl or Python code is too slow or too space-inefficient
- they have other code (written in some other language) that they want to
  incorporate into their program, so they have to write some C glue to
  make it all work
- they have to do something that can only be done in a low-level language,
  such as access to some system call; in a sense, this is the same as the
  previous point

One project I'm working on at the moment has about 3000 lines of
Python and about 400 lines of C.  The 400 lines of C allowed us to cut
our memory footprint by a factor of five.




More information about the Python-list mailing list