Python slow for filter scripts

David C. Fox davidcfox at post.harvard.edu
Tue Oct 28 17:43:35 EST 2003


Peter Mutsaers wrote:

> Hello,
> 
> Up to now I mostly wrote simple filter scripts in Perl, e.g.
> 
> while(<>) {
>   # do something with $_, regexp matching, replacements etc.
>   print;
> }
> 
> Now I learned Python and like it much more as a language.
> 
> However, I tried the most simple while(<>) {print;} in Perl versus
> Python, just a copy from stdin to stdout, to see how fast the basic
> filter can be.
> 
> I found that on my (linux) PC, the Python version was 4 times slower.
> 
> Is that normal, does it disqualify Python for simple filter scripts?

You don't show the Python script you use, so there's no way for us to 
tell whether it is possible to do it more efficiently.

Also, what size file did you use?  Unless you tried it with a large 
enough file, so that the time was proportional to the file size, you may 
just have measured the difference in the startup time for perl vs. python.

Finally, the relative performance of two languages on Task X is not a 
very good predictor of their relative performance on Task Y, so you are 
probably better off doing a comparison of the actual task you are 
interested in.

David





More information about the Python-list mailing list