Speed problems with Python vs. Perl

Bob Kline bkline at rksystems.com
Wed Mar 28 12:17:40 EST 2001


On Wed, 28 Mar 2001, Tim Hochberg wrote:

> I haven't actually benchmarked (or even tried) any of these, but I'd
> be curious to see how the last one in particular stacks up against
> it's Perl equivalent. I don't have Perl installed at the moment,
> though so I leave that as an excercise for the reader....

$ cat tsplit.py
#!/usr/bin/python
import sys, string

i = 0
for line in sys.stdin.readlines():
    i = i + 1
    f = string.split(line)

print "Total lines read: " + `i`
$ cat tsplit.pl
#!/usr/bin/perl
$icount = 0;

while(<>) {
  $icount++;
  @f = split;
}
print "Total lines read: $icount\n";
$ time ./tsplit.py < manual.txt
Total lines read: 32542

real    0m4.865s
user    0m4.780s
sys     0m0.090s
$ time ./tsplit.pl < manual.txt
Total lines read: 32542

real    0m4.452s
user    0m3.140s
sys     0m0.060s

Fairly comparable.  Don't have a version with xreadlines() yet, so I
can't say how much improvement that would add for the python times.

-- 
Bob Kline
mailto:bkline at rksystems.com
http://www.rksystems.com






More information about the Python-list mailing list