Yet another Python vs. Perl speed issue/question
Christian Tanzer
tanzer at swing.co.at
Wed Mar 14 01:54:51 EST 2001
nanotech at europa.com wrote:
> Python 2.0 on an HP (nice and big) takes ~30 seconds to lowercase all
> the lines in a 3meg file. Perl takes less than a second. What am I
> doing wrong?!?
>
> Python
> ------
> import sys
> import string
>
> stdin=sys.stdin
> lower=string.lower
>
> print map(lower,stdin.readlines())
>
> Perl
> ----
>
> print map(lc,<STDIN>);
Your Python code is doing unnecessary work (BTW, it doesn't do what
you expect). Just for comparison, the timings for a 3MB file on my
aging Pentium Pro 200 MHz machine:
$ time python /tmp/test1.py < /tmp/test.txt > /tmp/test.out
real 0m10.246s
user 0m4.810s
sys 0m1.190s
$ time python /tmp/test2.py < /tmp/test.txt > /tmp/test.out
real 0m0.781s
user 0m0.200s
sys 0m0.270s
#### test1.py #### test2.py
import sys import sys
import string import string
stdin=sys.stdin stdin=sys.stdin
lower=string.lower lower=string.lower
print map(lower,stdin.readlines()) sys.stdout.write(lower(stdin.read()))
Your perl code gives a compilation error:
$ time perl /tmp/test1.pl < /tmp/test.txt > /tmp/test.out
Not enough arguments for lower case at /tmp/test1.pl line 1, near "lc,"
Execution of /tmp/test1.pl aborted due to compilation errors.
Slightly changed, it lies halfway between the two python versions:
$ time perl /tmp/test2.pl < /tmp/test.txt > /tmp/test.out
real 0m3.297s
user 0m2.830s
sys 0m0.360s
#### test2.pl
print map {lc ($_)} <STDIN>;
--
Christian Tanzer tanzer at swing.co.at
Glasauergasse 32 Tel: +43 1 876 62 36
A-1130 Vienna, Austria Fax: +43 1 877 66 92
More information about the Python-list
mailing list