Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/src/cmd/perl/ext/Encode/t/unibench.pl

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!./perl

use strict;
use Encode;
use Benchmark qw(:all);

my $Count = shift @ARGV;
$Count ||= 16;
my @sizes = @ARGV || (1, 4, 16);

my %utf8_seed;
for my $i (0x00..0xff){
    my $c = chr($i);
    $utf8_seed{BMP} .= ($c =~ /^\p{IsPrint}/o) ? $c : " ";
}
utf8::upgrade($utf8_seed{BMP});

for my $i (0x00..0xff){
    my $c = chr(0x10000+$i);
    $utf8_seed{HIGH} .= ($c =~ /^\p{IsPrint}/o) ? $c : " ";
}
utf8::upgrade($utf8_seed{HIGH});

my %S;
for my $i (@sizes){
    my $sz = 256 * $i;
    for my $cp (qw(BMP HIGH)){
	$S{utf8}{$sz}{$cp}  = $utf8_seed{$cp} x $i;
	$S{utf16}{$sz}{$cp} = encode('UTF-16BE', $S{utf8}{$sz}{$cp});
    }
}

for my $i (@sizes){
    my $sz = $i * 256;
    my $count = $Count * int(256/$i);
    for my $cp (qw(BMP HIGH)){
	for my $op (qw(encode decode)){
	    my ($meth, $from, $to) = ($op eq 'encode') ?
		(\&encode, 'utf8', 'utf16') : (\&decode, 'utf16', 'utf8');
	    my $XS = sub {
		Encode::Unicode::set_transcoder("xs");  
		$meth->('UTF-16BE', $S{$from}{$sz}{$cp})
		     eq $S{$to}{$sz}{$cp} 
			 or die "$op,$from,$to,$sz,$cp";
	    };
	    my $modern = sub {
		Encode::Unicode::set_transcoder("modern");  
		$meth->('UTF-16BE', $S{$from}{$sz}{$cp})
		     eq $S{$to}{$sz}{$cp} 
			 or die "$op,$from,$to,$sz,$cp";
	    };
	    my $classic = sub {
		Encode::Unicode::set_transcoder("classic");  
		$meth->('UTF-16BE', $S{$from}{$sz}{$cp})
		     eq $S{$to}{$sz}{$cp} or 
			 die "$op,$from,$to,$sz,$cp";
	    };
	    print "---- $op length=$sz/range=$cp ----\n";
	    my $r = timethese($count,
		     {
		      "XS"      => $XS,
		      "Modern"  => $modern,
		      "Classic" => $classic,
		     },
		     'none',
		    );
	    cmpthese($r);
	}
    }
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.