私はperlにかなり慣れていません。それで、明白な答えがあれば申し訳ありません。私の質問: C++ の std::partial_sort の Perl に組み込みの代替手段はありますか。または、少なくとも、このアルゴリズムを実装する CPAN モジュールをお勧めできますか? 前もって感謝します。
2 に答える
3
あなたが望むのは のようですSort::Key::Top
。
sort
説明したように Perl の選択ソートを作成しました。リスト全体を使用してトップ 10 を選択するよりも 4 倍以上高速であることが判明しましたが、top
関数 fromSort::Key::Top
は再び 2 倍以上高速です。
これが私のコードと結果です。AAAA
からZZZZ
50 万近くの 4 文字パターンのリストをテストします。
use strict;
use warnings;
use List::Util 'shuffle';
use Sort::Key::Top 'top';
use Benchmark 'timethese';
srand(0);
my @list = shuffle 'AAAA' .. 'ZZZZ';
timethese(100, {
'Sort::Key::Top' => sub {
my @topten = top 10 => @list;
},
'Pure Perl' => sub {
my @topten;
for my $item (@list) {
if (@topten and $item lt $topten[-1]) {
my $i = $#topten-1;
--$i while $i >= 0 and $topten[$i] gt $item;
splice @topten, $i+1, 0, $item;
pop @topten if @topten > 10;
}
elsif (@topten < 10) {
push @topten, $item;
}
}
},
'Perl sort' => sub {
my @topten = (sort @list)[0..9];
},
});
出力
Benchmark: timing 100 iterations of Perl sort, Pure Perl, Sort::Key::Top...
Perl sort: 46 wallclock secs (45.76 usr + 0.11 sys = 45.86 CPU) @ 2.18/s (n=100)
Pure Perl: 11 wallclock secs (10.84 usr + 0.00 sys = 10.84 CPU) @ 9.22/s (n=100)
Sort::Key::Top: 4 wallclock secs ( 3.99 usr + 0.13 sys = 4.12 CPU) @ 24.28/s (n=100)
于 2013-02-24T22:35:52.757 に答える
1
クイック検索が表示されますSort::Key::Top
が、他の選択肢がある場合があります。
于 2013-02-24T11:35:38.077 に答える