我有一个大约 100,000 个元素的字符串数组。我需要遍历每个元素并用其他词替换一些词。在纯 perl 中这需要几秒钟。我需要尽可能加快速度。我正在使用以下代码段进行测试:
use strict;
my $string = "This is some string. Its only purpose is for testing.";
for( my $i = 1; $i < 100000; $i++ ) {
$string =~ s/old1/new1/ig;
$string =~ s/old2/new2/ig;
$string =~ s/old3/new3/ig;
$string =~ s/old4/new4/ig;
$string =~ s/old5/new5/ig;
}
我知道这实际上并没有替换测试字符串中的任何内容,但它仅用于速度测试。
我寄希望于Inline::C。我以前从未使用过,Inline::C
但在阅读了一点之后,我认为它实现起来相当简单。但显然,即使调用一个什么都不做的存根函数也会慢很多。这是我测试的片段:
use strict;
use Benchmark qw ( timethese );
use Inline 'C';
timethese(
5,
{
"Pure Perl" => \&pure_perl,
"Inline C" => \&inline_c
}
);
sub pure_perl {
my $string = "This is some string. Its only purpose is for testing.";
for( my $i = 1; $i < 1000000; $i++ ) {
$string =~ s/old1/new1/ig;
$string =~ s/old2/new2/ig;
$string =~ s/old3/new3/ig;
$string =~ s/old4/new4/ig;
$string =~ s/old5/new5/ig;
}
}
sub inline_c {
my $string = "This is some string. Its only purpose is for testing.";
for( my $i = 1; $i < 1000000; $i++ ) {
$string = findreplace( $string, "old1", "new1" );
$string = findreplace( $string, "old2", "new2" );
$string = findreplace( $string, "old3", "new3" );
$string = findreplace( $string, "old4", "new4" );
$string = findreplace( $string, "old5", "new5" );
}
}
__DATA__
__C__
char *
findreplace( char *text, char *what, char *with ) {
return text;
}
在我的 Linux 机器上,结果是:
Benchmark: timing 5 iterations of Inline C, Pure Perl...
Inline C: 6 wallclock secs ( 5.51 usr + 0.02 sys = 5.53 CPU) @ 0.90/s (n=5)
Pure Perl: 2 wallclock secs ( 2.51 usr + 0.00 sys = 2.51 CPU) @ 1.99/s (n=5)
纯 Perl 的速度是调用空 C 函数的两倍。完全不是我所期望的!同样,我以前从未使用过 Inline::C,所以也许我在这里遗漏了一些东西?