我在perl中的性能有问题。这是代码:http://pastebin.com/jpmhv395
它可能在其他地方也有问题,但是主要问题在第336行中:Anagram_hash方法似乎经常被称为。该方法实际上是在不同的模块中,这里是:http://pastebin.com/5nrc4bs8
子例程的工作方式应取决于整数还是字符串作为参数。
。子例程" Anagram_hash"会导致性能差,还是看到其他可能导致性能下降的东西?如果是这样,如何优化它?
我想您可以制作一个256个元素查找表,以便您只做
$result += $lookup{$char};
而不是
my $temp = ord($char);
$result += $temp**5;
,但是您应该真正运行探索器,以查看问题首先...
编辑(JM666和Ikegami) - 添加了基准示例。如您所见,通过观察power_goodloop和lookup_goodloop的结果,仅使用指示或哈希查找而有所不同,启用速度要快得多。
是不良的循环使您放慢脚步。use strict;
use warnings;
use feature qw( say );
use Benchmark qw(:all);
my @lookup = map { $_ ** 5 } 0..255;
my %lookup = map { chr($_) => $_ ** 5 } 0..255;
my $str = join '', map chr(rand(256)), 1..1000;
say "test of the result";
say anagram_hash1($str);
say anagram_hash2($str);
say anagram_hash3($str);
say anagram_hash4($str);
say anagram_hash5($str);
say "";
cmpthese(-3, {
'power_badloop' => sub { anagram_hash1($str) },
'hlookup_badloop' => sub { anagram_hash2($str) },
'power_goodloop' => sub { anagram_hash3($str) },
'hlookup_goodloop' => sub { anagram_hash4($str) },
'alookup_goodloop' => sub { anagram_hash5($str) },
});
sub anagram_hash1 {
my $result = 0;
my $s = shift;
my $length = length($s);
if ( $s =~ /[a-zA-Z]+/ ) {
for ( my $i = 0 ; $i < $length ; $i++ ) {
my $char = substr( $s, $i, 1 );
my $temp = ord($char);
$result += $temp**5;
}
} elsif ( $s =~ /^[d]+$/ ) {
my $temp = int($s);
$result += $temp**5;
} else {
die "Invalid parameter passed to method 'anagram_hash'nExpected: String or NumbernPassed: $s";
}
return $result;
}
sub anagram_hash2 {
my $result = 0;
my $s = shift;
my $length = length($s);
if ( $s =~ /[a-zA-Z]+/ ) {
for ( my $i = 0 ; $i < $length ; $i++ ) {
my $char = substr( $s, $i, 1 );
$result += $lookup{$char};
}
} elsif ( $s =~ /^[d]+$/ ) {
my $temp = int($s);
$result += $temp**5;
} else {
die "Invalid parameter passed to method 'anagram_hash'nExpected: String or NumbernPassed: $s";
}
return $result;
}
sub anagram_hash3 {
my $result = 0;
my $s = shift;
if ( $s =~ /[a-zA-Z]/ ) {
$result += $_ ** 5 for unpack "C*", $s;
} elsif ( $s =~ /^[d]+$/ ) {
$result += int($s) ** 5;
} else {
die "Invalid parameter passed to method 'anagram_hash'nExpected: String or NumbernPassed: $s";
}
return $result;
}
sub anagram_hash4 {
my $result = 0;
my $s = shift;
if ( $s =~ /[a-zA-Z]/ ) {
$result += $lookup{$_} for unpack "(a)*", $s;
} elsif ( $s =~ /^[d]+$/ ) {
$result += int($s) ** 5;
} else {
die "Invalid parameter passed to method 'anagram_hash'nExpected: String or NumbernPassed: $s";
}
return $result;
}
sub anagram_hash5 {
my $result = 0;
my $s = shift;
if ( $s =~ /[a-zA-Z]/ ) {
$result += $lookup[$_] for unpack "C*", $s;
} elsif ( $s =~ /^[d]+$/ ) {
$result += int($s) ** 5;
} else {
die "Invalid parameter passed to method 'anagram_hash'nExpected: String or NumbernPassed: $s";
}
return $result;
}
输出:
test of the result
171658778879381
171658778879381
171658778879381
171658778879381
171658778879381
Rate power_badloop hlookup_badloop hlookup_goodloop power_goodloop alookup_goodloop
power_badloop 2132/s -- -25% -35% -71% -74%
hlookup_badloop 2826/s 33% -- -14% -62% -66%
hlookup_goodloop 3294/s 55% 17% -- -56% -60%
power_goodloop 7446/s 249% 163% 126% -- -10%
alookup_goodloop 8298/s 289% 194% 152% 11% --
所以,结果显示:
- 原始OP的代码是最慢的
- 第二个是Mark的解决方案(用Hash查找替换ORD/EXP) - 因此,Mark的解决方案比原始OP的代码快。
最后,(通常)ikegami带有3个解决方案, em 的速度都比以前的任何一个快。:)