我对以下 Perl 模块进行了基准测试:
- Math::Combinatorics
- Algorithm::Combinatorics
- Cmb
基准测试包括执行 OP 要求的操作,组合 2 个项目,但将单词集增加到 10,000 个,而不是最初请求的 5 个 (AAA BBB CCC DDD EEE)。
Math::Combinatorics 的测试脚本
#!/usr/bin/env perl
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 的测试脚本
#!/usr/bin/env perl
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 的测试脚本
#!/usr/bin/env perl
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 实际上可以比上面的更快。
#!/usr/bin/env perl
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 版本 7.7.1908 (Core) 上使用 dpv 在内核 3.10.0-1062.1.1.el7.x86_64 x86_64 上使用 Perl 5.16.3 在 Intel(R) Xeon(R) CPU 上执行的E5-2699 v4 @ 2.20GHz