xref: /openbsd-src/gnu/usr.bin/perl/cpan/Encode/t/unibench.pl (revision b39c515898423c8d899e35282f4b395f7cad3298)
1*b39c5158Smillert#!./perl
2*b39c5158Smillert
3*b39c5158Smillertuse strict;
4*b39c5158Smillertuse Encode;
5*b39c5158Smillertuse Benchmark qw(:all);
6*b39c5158Smillert
7*b39c5158Smillertmy $Count = shift @ARGV;
8*b39c5158Smillert$Count ||= 16;
9*b39c5158Smillertmy @sizes = @ARGV || (1, 4, 16);
10*b39c5158Smillert
11*b39c5158Smillertmy %utf8_seed;
12*b39c5158Smillertfor my $i (0x00..0xff){
13*b39c5158Smillert    my $c = chr($i);
14*b39c5158Smillert    $utf8_seed{BMP} .= ($c =~ /^\p{IsPrint}/o) ? $c : " ";
15*b39c5158Smillert}
16*b39c5158Smillertutf8::upgrade($utf8_seed{BMP});
17*b39c5158Smillert
18*b39c5158Smillertfor my $i (0x00..0xff){
19*b39c5158Smillert    my $c = chr(0x10000+$i);
20*b39c5158Smillert    $utf8_seed{HIGH} .= ($c =~ /^\p{IsPrint}/o) ? $c : " ";
21*b39c5158Smillert}
22*b39c5158Smillertutf8::upgrade($utf8_seed{HIGH});
23*b39c5158Smillert
24*b39c5158Smillertmy %S;
25*b39c5158Smillertfor my $i (@sizes){
26*b39c5158Smillert    my $sz = 256 * $i;
27*b39c5158Smillert    for my $cp (qw(BMP HIGH)){
28*b39c5158Smillert    $S{utf8}{$sz}{$cp}  = $utf8_seed{$cp} x $i;
29*b39c5158Smillert    $S{utf16}{$sz}{$cp} = encode('UTF-16BE', $S{utf8}{$sz}{$cp});
30*b39c5158Smillert    }
31*b39c5158Smillert}
32*b39c5158Smillert
33*b39c5158Smillertfor my $i (@sizes){
34*b39c5158Smillert    my $sz = $i * 256;
35*b39c5158Smillert    my $count = $Count * int(256/$i);
36*b39c5158Smillert    for my $cp (qw(BMP HIGH)){
37*b39c5158Smillert    for my $op (qw(encode decode)){
38*b39c5158Smillert        my ($meth, $from, $to) = ($op eq 'encode') ?
39*b39c5158Smillert        (\&encode, 'utf8', 'utf16') : (\&decode, 'utf16', 'utf8');
40*b39c5158Smillert        my $XS = sub {
41*b39c5158Smillert        Encode::Unicode::set_transcoder("xs");
42*b39c5158Smillert        $meth->('UTF-16BE', $S{$from}{$sz}{$cp})
43*b39c5158Smillert             eq $S{$to}{$sz}{$cp}
44*b39c5158Smillert             or die "$op,$from,$to,$sz,$cp";
45*b39c5158Smillert        };
46*b39c5158Smillert        my $modern = sub {
47*b39c5158Smillert        Encode::Unicode::set_transcoder("modern");
48*b39c5158Smillert        $meth->('UTF-16BE', $S{$from}{$sz}{$cp})
49*b39c5158Smillert             eq $S{$to}{$sz}{$cp}
50*b39c5158Smillert             or die "$op,$from,$to,$sz,$cp";
51*b39c5158Smillert        };
52*b39c5158Smillert        my $classic = sub {
53*b39c5158Smillert        Encode::Unicode::set_transcoder("classic");
54*b39c5158Smillert        $meth->('UTF-16BE', $S{$from}{$sz}{$cp})
55*b39c5158Smillert             eq $S{$to}{$sz}{$cp} or
56*b39c5158Smillert             die "$op,$from,$to,$sz,$cp";
57*b39c5158Smillert        };
58*b39c5158Smillert        print "---- $op length=$sz/range=$cp ----\n";
59*b39c5158Smillert        my $r = timethese($count,
60*b39c5158Smillert             {
61*b39c5158Smillert              "XS"      => $XS,
62*b39c5158Smillert              "Modern"  => $modern,
63*b39c5158Smillert              "Classic" => $classic,
64*b39c5158Smillert             },
65*b39c5158Smillert             'none',
66*b39c5158Smillert            );
67*b39c5158Smillert        cmpthese($r);
68*b39c5158Smillert    }
69*b39c5158Smillert    }
70*b39c5158Smillert}
71