public
Description: Move-to-front transform encoder/decoder
Homepage:
Clone URL: git://github.com/naoya/perl-algorithm-mtf.git
perl-algorithm-mtf / bench.pl
100644 46 lines (37 sloc) 0.98 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin::libs;
 
use Perl6::Say;
use Algorithm::DivSufSort;
use Benchmark;
use Path::Class qw/file/;
 
use Algorithm::MTF;
use Algorithm::MTF::List;
use Algorithm::MTF::Array;
 
sub bs_encode ($) {
    my $text = shift;
    my $sa = divsufsort( $text );
 
    my @text = unpack('C*', $text);
    return join '', map { chr $text[$_ -1] } @$sa;
}
 
my $bwt = bs_encode file('/etc/httpd/conf/mime.types')->slurp;
 
my $list = Algorithm::MTF::List->new;
for (my $i = 0xff; $i >= 0; $i--) {
    $list->insert(chr $i);
    # $list->insert( Algorithm::MTF::List::Node->new(chr $i) );
}
my $list_mtf = Algorithm::MTF::Encoder->new($list);
 
my $array = Algorithm::MTF::Array->new;
for (my $i = 0; $i < 0x100; $i++) {
    $array->[$i] = chr $i;
}
my $array_mtf = Algorithm::MTF::Encoder->new($array);
 
timethese(10, {
    list => sub {
        $list_mtf->encode($bwt);
    },
    # array => sub {
    # $array_mtf->encode($bwt);
    # },
});