awk, python, perl文本处理效率对比

11584阅读 5评论2011-02-14 wcw
分类:

以下3个文件依次是用python、awk和perl写的脚本,做同一件事情:
diff.sh f1 f2
f1和f2每一行的第一个字段(以空格分割)为key,如果f2某一行的key在f1中不存在,则输出f2该行。

比如:
a.dat的内容是
1 a
2 a

b.dat的内容是
1 b
3 b

那么diff.sh a.dat b.dat则输出
3 b

代码:
diff.py

#!/usr/bin/python
import sys
if len(sys.argv) != 3:
    print "Usage: " + sys.argv[0] + "file1 file2";
    sys.exit(-1);

file1 = sys.argv[1]
file2 = sys.argv[2]

list1 = {};
for line in open(file1):
    list1[line.split()[0]] = 1;

for line in open(file2):
    key = line.split()[0];
    if key not in list1:
        sys.stdout.write(line)


diff.sh

#!/bin/sh
if [[ $# < 2 ]];then
    echo "Usage: $0 file1 file2"
    exit
fi

function do_diff()
{
    if [[ $# < 2 ]];then
        echo "Usage: $0 file1 file2"
        return 1
    fi
    if [[ ! -f $1 ]];then
        echo "$1 is not file"
        return 2
    fi
    if [[ ! -f $2 ]];then
        echo "$2 is not file"
        return 3
    fi

    awk '
        BEGIN{FS=OFS=" "}
        ARGIND == 1 {
            arr[$1] = 1;
        }
        ARGIND == 2 {
            if (!($1 in arr)) {
                print $0;
            }
        }
    '
$1 $2
}

do_diff $1 $2


diff.pl

#!/usr/bin/perl -w
exit if (1 > $#ARGV);

my %map_orig;

my $file_orig = shift @ARGV;
open FH, "<$file_orig" or die "can't open file: $file_orig";
while (<FH>) {
        chomp;
        #$map_orig{$_} = 1;
        my ($filed) = split /\s+/;
        $map_orig{$filed} = 1;
}
close (FH);

my $file_diff = shift @ARGV;
open FH, "<$file_diff" or die "can't open file: $file_diff";
while (<FH>) {
        chomp;
        my ($filed) = split /\s+/;
        print "$_\n" if (!defined$map_orig{$filed});
}
close (FH)



diff2.pl

#!/usr/bin/perl -w
exit if (1 > $#ARGV);

my %map_orig;

my $file_orig = shift @ARGV;
open FH, "<$file_orig" or die "can't open file: $file_orig";
while (<FH>) {
        chomp;
        #$map_orig{$_} = 1;
        my ($filed) = split(" ");
        $map_orig{$filed} = 1;
}
close (FH);

my $file_diff = shift @ARGV;
open FH, "<$file_diff" or die "can't open file: $file_diff";
while (<FH>) {
        chomp;
        my ($filed) = split(" ");
        print "$_\n" if (!defined$map_orig{$filed});
}
close (FH)

以上4个文件的算法都是一样的,把第一个文件的key读取放到一个map中,再读取第2个文件的key,判断是否在该map中,不是则打印到标准输出。diff.pl和diff2.pl的区别是前者用了正则,后者是用字符串匹配。

测试方法:time diff.xx f1 f2 > out

测试文件f1有123183923行,每一行格式为:
key value(两个字段)
文件大小为2.5G

f2有439116行,每一行的格式也是:
key value(两字段)
文件大小为5.6M

测试结果(time real):
diff.py的时间为3m46s = 226s
diff.sh的时间为3m49s = 229s
diff.pl的时间为(7m21s + 7m12s) = 437s
diff2.pl的时间为(7m41s + 7m34s)/2 = 454s
结果显示awk和python的性能差不多,perl则要明显差些。看来python的dict优化得很好,居然能赶上awk的性能,很出乎我的意料。

以上测试在同一台机器上跑,测试环境一样,但非严格公平(不同时间内机器负载等可能略有不同)。

--------------
有人质疑diff.pl是用了正则导致了效率降低,于是我取消了正则,用简单的字符串匹配,结果性能并没有预期的得到提升,相反,甚至有了一点点下降。


上一篇:ssh登录失败
下一篇:博客已升级,请注意变更地址

文章评论