提高Python代码性能

如何提高这个简单的
python代码的性能?

不是re.search是寻找匹配线的最佳方法,因为它比Perl慢近6倍,或者我做错了什么?

#!/usr/bin/env python

import re
import time
import sys

i=0
j=0
time1=time.time()
base_register =r'DramBaseAddress\d+'
for line in  open('rndcfg.cfg'):
    i+=1
    if(re.search(base_register, line)):
        j+=1
time2=time.time()

print (i,j)
print (time2-time1)    
print (sys.version)

此代码大约需要0.96秒才能完成(平均10次运行)
输出:

168197 2688
0.8597519397735596
3.3.2 (default, Sep 24 2013, 15:14:17)
[GCC 4.1.1]

而以下Perl代码在0.15秒内完成.

#!/usr/bin/env perl
use strict;
use warnings;

use Time::HiRes qw(time);

my $i=0;my $j=0;
my $time1=time;
open(my $fp, 'rndcfg.cfg');
while(<$fp>)
{
    $i++;
    if(/DramBaseAddress\d+/)
    {
        $j++;
    }
}
close($fp);
my $time2=time;

printf("%d,%d\n",$i,$j);
printf("%f\n",$time2-$time1);
printf("%s\n",$]);

输出:

168197,2688
0.135579
5.012001

编辑:更正了正则表达式 – 这使性能略有恶化

最佳答案 实际上,正则表达式的效率低于Python中的字符串方法.从
https://docs.python.org/2/howto/regex.html#use-string-methods开始:

Strings have several methods for performing operations with fixed
strings and they’re usually much faster, because the implementation is
a single small C loop that’s been optimized for the purpose, instead
of the large, more generalized regular expression engine.

用str.find替换re.search将为您提供更好的运行时.否则,使用其他人建议的in运算符也会进行优化.

至于Python与之间的速度差异Perl版本,我只是简单介绍每种语言的内在质量:text processing – python vs perl performance

点赞