我进行了以下Perl模块的基准测试:
- Math::Combinatorics
- Algorithm::Combinatorics
- Cmb
基准测试包括执行OP要求的内容,即组合2个项目,但将单词集合从原始请求的5个(AAA BBB CCC DDD EEE)增加到10,000个。
Math::Combinatorics的测试脚本
use strict; use warnings;
use Math::Combinatorics;
my $strings = [qw(AAA BBB CCC DDD EEE) x 2000];
my $iter = new Math::Combinatorics (count => 2, data => $strings);
while (my @c = $iter->next_combination) {
print "@c\n";
}
这产生了每秒约53,479个组合。
Algorithm::Combinatorics的测试脚本
use strict; use warnings;
use Algorithm::Combinatorics qw(combinations);
my $strings = [qw(AAA BBB CCC DDD EEE) x 2000];
my $iter = combinations($strings, 2);
while (my $c = $iter->next) {
print "@$c\n";
}
这产生了每秒约861,982个组合。
Cmb的测试脚本
use strict; use warnings;
use Cmb;
my $strings = [qw(AAA BBB CCC DDD EEE) x 2000];
my $cmb = new Cmb { size_min => 2, size_max => 2 };
$cmb->cmb_callback($#$strings + 1, $strings, sub {
print "@_\n";
return 0;
});
这产生了大约每秒2,940,882种组合。
但如果您只需要打印组合,Cmb实际上可以比上述方法更快地完成。
use strict; use warnings;
use Cmb;
my $strings = [qw(AAA BBB CCC DDD EEE) x 2000];
my $cmb = new Cmb { size_min => 2, size_max => 2 };
$cmb->cmb($#$strings + 1, $strings);
这产生了大约每秒3,333,000个组合。
基准测试是在CentOS Linux release 7.7.1908 (Core)下,使用Perl 5.16.3在Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz上运行,内核版本为3.10.0-1062.1.1.el7.x86_64 x86_64,使用
dpv。