# wc baseline output for comparison: ~$ wc perl_wc.pl pyth_wc.py large.txt 19 58 492 perl_wc.pl 25 96 673 pyth_wc.py 208048 1675832 11021496 large.txt 208092 1675986 11022661 total ~$ cat perl_wc.pl pyth_wc.py large.txt | wc 208092 1675986 11022661 # python version 2.0.1 ~$ time ./pyth_wc.py perl_wc.pl pyth_wc.py large.txt 19 58 492 perl_wc.pl 25 96 673 pyth_wc.py 208048 1675832 11021496 large.txt 208092 1675986 11022661 total real 0m31.360s user 0m31.030s sys 0m0.090s :~$ cat perl_wc.pl pyth_wc.py |./pyth_wc.py 44 154 1165 # perl version 5.6.1 ~$ time ./perl_wc.pl perl_wc.pl pyth_wc.py large.txt 19 58 492 perl_wc.pl 25 96 673 pyth_wc.py 208048 1675832 11021496 large.txt 208092 1675986 11022661 total real 0m7.450s user 0m7.240s sys 0m0.090s ~$ cat perl_wc.pl pyth_wc.py |./perl_wc.pl 44 154 1165 - #### #!/usr/bin/python import sys files = map( lambda f: open(f), sys.argv[1:]) or [sys.stdin] Twords, Tlines, Tchars = 0, 0, 0 for file in files: words, lines, chars = 0, 0, 0 while 1: line = file.readline() if line: lines = lines + 1 list = line.split() words = words + len(list) chars = chars + len(line) else: print "%7d %7d %7d %s" % (lines, words, chars, file.name) break file.close() Twords = Twords + words Tlines = Tlines + lines Tchars = Tchars + chars if len(sys.argv) > 2: print "%7d %7d %7d total" % (Tlines, Twords, Tchars) #!/usr/bin/perl -w use strict; my $total = @ARGV > 1; my($Tlines, $Twords, $Tbytes,$lines, $words, $bytes); while(<>){ my @words = split; $words += @words; $bytes += length; $lines++; if (eof) { printf "%7d %7d %7d %s\n",$lines,$words,$bytes,$ARGV; $Tlines += $lines; $Twords += $words; $Tbytes += $bytes; ($lines,$bytes,$words) = (0,0,0); close ARGV; } } printf "%7d %7d %7d total\n",$Tlines,$Twords,$Tbytes if $total;