对utf8文件进行基准读取 - 解释差异

jm6*_*666 10 io perl encode utf-8

有这个代码:

#!/usr/bin/env perl

use 5.016;
use warnings;
use autodie;
use Path::Tiny;
use Encode;
use Benchmark qw(:all);

my $cnt = 10_000;
my $utf = 'utf8.txt';

my $res = timethese($cnt, {
    'open-UTF-8' => sub {
        open my $fhu, '<:encoding(UTF-8)', $utf;
        my $stru = do { local $/; <$fhu>};
        close $fhu;
    },
    'open-utf8' => sub {
        open my $fhu, '<:utf8', $utf;
        my $stru = do { local $/; <$fhu>};
        close $fhu;
    },
    'decode-utf8' => sub {
        open my $fhu, '<', $utf;
        my $stru = decode('utf8', do { local $/; <$fhu>});
        close $fhu;
    },
    'decode-UTF-8' => sub {
        open my $fhu, '<', $utf;
        my $stru = decode('UTF-8', do { local $/; <$fhu>});
        close $fhu;
    },
    'ptiny' => sub {
        my $stru = path($utf)->slurp_utf8;
    },
});
cmpthese $res;
Run Code Online (Sandbox Code Playgroud)

utf8.txt(大约175KB)含有1000线UTF8编码/ ASCII字符,等的:

9áä??é?í???óôö???š?ú?ü?ýž ÁÄ??É?Í???ÓÔÖ???Š?Ú?Ü?ÝŽ aáäbc?d?eé?fghiíjk??mn?oóôö?pqr??sšt?uú?ü?vwxyýzž
Run Code Online (Sandbox Code Playgroud)

运行上面的内容,在我的笔记本上给出:

Benchmark: timing 10000 iterations of decode-UTF-8, decode-utf8, open-UTF-8, open-utf8, ptiny...
decode-UTF-8: 47 wallclock secs (46.83 usr +  0.87 sys = 47.70 CPU) @ 209.64/s (n=10000)
 decode-utf8: 48 wallclock secs (46.62 usr +  0.90 sys = 47.52 CPU) @ 210.44/s (n=10000)
  open-UTF-8: 60 wallclock secs (57.82 usr +  1.20 sys = 59.02 CPU) @ 169.43/s (n=10000)
   open-utf8:  7 wallclock secs ( 6.57 usr +  0.70 sys =  7.27 CPU) @ 1375.52/s (n=10000)
       ptiny:  7 wallclock secs ( 5.98 usr +  0.52 sys =  6.50 CPU) @ 1538.46/s (n=10000)
               Rate  open-UTF-8 decode-UTF-8 decode-utf8   open-utf8       ptiny
open-UTF-8    169/s          --         -19%        -19%        -88%        -89%
decode-UTF-8  210/s         24%           --         -0%        -85%        -86%
decode-utf8   210/s         24%           0%          --        -85%        -86%
open-utf8    1376/s        712%         556%        554%          --        -11%
ptiny        1538/s        808%         634%        631%         12%          --
Run Code Online (Sandbox Code Playgroud)

对我来说令人惊讶,所以问题:

  • 首先 - 上面的代码有问题吗?

如果还可以,

  • 为什么显性UTF-8和放松之间存在巨大差异,utf8但仅限于IO层级(<:utf8<:encoding(UTF-8)?所以,
  • 为什么差异不那么大的时候decode('UTF-8'decode('utf8'
  • 为什么懒惰的IO层级解码要快得多,因为显式也懒惰decode('utf8
  • 什么"危险"可以使用放松(快速)"utf8"与精确(慢)'UTF-8'?

  • 最后,不是一个真正的问题 - 我必须检查Path :: Tiny代码 - 它是如何最快的...

ENV:

  • perl v5.22.0 - perlbrew(线程)
  • OSX - Darwin内核版本14.4.0 :(约塞米蒂)
  • 笔记本电脑老款 - MacBook Pro(13英寸,2010年中) - 核心2双核,2.4Ghz,8GB,慢硬盘

cha*_*sen 8

:UTF8

PerlIO :utf8层是伪层,它只是OP检测到的PerlIO句柄上的标志.行为因使用的OP而异:

read(),sysread()和recv():

实现不执行utf8序列的验证.实施仅检查 utf8序列的前缀八位字节以计算读取的utf8序列的数量.

的ReadLine():

实施验证读取八位字节如果警告'utf8'是效果,如果读个字节包含形成不良的UTF8发出警告.使用的验证程序与使用的相同utf8::decode().

除非您愿意接受可能导致安全问题的错误形成的UTF-X,否则不应使用':utf8'标志/图层进行读取或分段错误的错误.

:编码

PerlIO :encoding层由PerlIO :: encoding提供,它为Encode :: Encoding的子类实现增量解码器框架.该实现通过为每个增量解码调用一个方法来调用Perl/XS子类.缓冲区在图层和子类之间复制.

utf8 vs UTF-8

utf8编码格式是Unicode Consortium 指定的UTF-8编码格式的超集.utf8编码格式接受以UTF-8编码形式格式错误的编码代码点,例如U + 10FFFF以上的代理和代码点.即使最近更改了 Unicode,也应该避免使用非字符了主意.utf8编码不应该用于交换,它是Perl的内部编码.请改用UTF-8编码表单.

啜饮UTF-8编码文件的基准

基准测试中使用的模块:

PerlIO :: encoding,PerlIO :: utf8_strict,EncodeUnicode :: UTF8.

gist.github.com上也提供以下代码.

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark     qw[];
use Config        qw[%Config];
use IO::Dir       qw[];
use IO::File      qw[SEEK_SET];

use Encode              qw[];
use Unicode::UTF8       qw[];
use PerlIO::encoding    qw[];
use PerlIO::utf8_strict qw[];

# https://github.com/chansen/p5-unicode-utf8/tree/master/benchmarks/data
my $dir  = 'benchmarks/data';
my @docs = do {
    my $d = IO::Dir->new($dir)
      or die qq/Could not open directory '$dir': $!/;
    sort grep { /^[a-z]{2}\.txt/ } $d->read;
};

printf "perl:                %s (%s %s)\n", $], @Config{qw[osname osvers]};
printf "Encode:              %s\n", Encode->VERSION;
printf "Unicode::UTF8:       %s\n", Unicode::UTF8->VERSION;
printf "PerlIO::encoding:    %s\n", PerlIO::encoding->VERSION;
printf "PerlIO::utf8_strict: %s\n", PerlIO::utf8_strict->VERSION;

foreach my $doc (@docs) {

    my $octets = do {
        open my $fh, '<:raw', "$dir/$doc" or die $!;
        local $/; <$fh>;
    };

    my $string = Unicode::UTF8::decode_utf8($octets);

    my @ranges = (
        [    0x00,     0x7F, qr/[\x{00}-\x{7F}]/        ],
        [    0x80,    0x7FF, qr/[\x{80}-\x{7FF}]/       ],
        [   0x800,   0xFFFF, qr/[\x{800}-\x{FFFF}]/     ],
        [ 0x10000, 0x10FFFF, qr/[\x{10000}-\x{10FFFF}]/ ],
    );

    my @out;
    foreach my $r (@ranges) {
        my ($start, $end, $regexp) = @$r;
        my $count = () = $string =~ m/$regexp/g;
        push @out, sprintf "U+%.4X..U+%.4X: %d", $start, $end, $count
          if $count;
    }

    printf "\n\n%s: Size: %d Code points: %d (%s)\n",
      $doc, length $octets, length $string, join ' ', @out;

    open my $fh_raw, '<:raw', \$octets 
      or die qq/Could not open a :raw fh: '$!'/;
    open my $fh_encoding, '<:encoding(UTF-8)', \$octets
      or die qq/Could not open a :encoding fh: '$!'/;
    open my $fh_utf8_strict, '<:utf8_strict', \$octets 
      or die qq/Could not open a :utf8_strict fh: '$!'/;

    Benchmark::cmpthese( -10, {
        ':encoding(UTF-8)' => sub {
            my $data = do { local $/; <$fh_encoding> };
            seek($fh_encoding, 0, SEEK_SET)
              or die qq/Could not rewind fh: '$!'/;
        },
        ':utf8_strict' => sub {
            my $data = do { local $/; <$fh_utf8_strict> };
            seek($fh_utf8_strict, 0, SEEK_SET)
              or die qq/Could not rewind fh: '$!'/;
        },
        'Encode' => sub {
            my $data = Encode::decode('UTF-8', do { local $/; scalar <$fh_raw> }, Encode::FB_CROAK|Encode::LEAVE_SRC);
            seek($fh_raw, 0, SEEK_SET)
             or die qq/Could not rewind fh: '$!'/;
        },        
        'Unicode::UTF8' => sub {
            my $data = Unicode::UTF8::decode_utf8(do { local $/; scalar <$fh_raw> });
            seek($fh_raw, 0, SEEK_SET)
             or die qq/Could not rewind fh: '$!'/;
        },
    });
}
Run Code Online (Sandbox Code Playgroud)

结果:

$ perl benchmarks/slurp.pl 
perl:                5.023001 (darwin 14.4.0)
Encode:              2.75
Unicode::UTF8:       0.60
PerlIO::encoding:    0.21
PerlIO::utf8_strict: 0.006


ar.txt: Size: 25918 Code points: 14308 (U+0000..U+007F: 2698 U+0080..U+07FF: 11610)
                    Rate :encoding(UTF-8)      Encode :utf8_strict Unicode::UTF8
:encoding(UTF-8)  3058/s               --        -19%         -73%          -87%
Encode            3754/s              23%          --         -67%          -84%
:utf8_strict     11361/s             272%        203%           --          -52%
Unicode::UTF8    23620/s             672%        529%         108%            --


el.txt: Size: 103974 Code points: 58748 (U+0000..U+007F: 13560 U+0080..U+07FF: 45150 U+0800..U+FFFF: 38)
                   Rate :encoding(UTF-8)       Encode :utf8_strict Unicode::UTF8
:encoding(UTF-8)  780/s               --         -19%         -73%          -86%
Encode            958/s              23%           --         -66%          -83%
:utf8_strict     2855/s             266%         198%           --          -48%
Unicode::UTF8    5498/s             605%         474%          93%            --


en.txt: Size: 82171 Code points: 82055 (U+0000..U+007F: 81988 U+0080..U+07FF: 18 U+0800..U+FFFF: 49)
                    Rate :encoding(UTF-8)      Encode :utf8_strict Unicode::UTF8
:encoding(UTF-8)  1111/s               --        -16%         -90%          -96%
Encode            1327/s              19%          --         -88%          -95%
:utf8_strict     11446/s             931%        763%           --          -60%
Unicode::UTF8    28635/s            2478%       2058%         150%            --


ja.txt: Size: 180109 Code points: 64655 (U+0000..U+007F: 6913 U+0080..U+07FF: 30 U+0800..U+FFFF: 57712)
                   Rate :encoding(UTF-8)       Encode :utf8_strict Unicode::UTF8
:encoding(UTF-8)  553/s               --         -27%         -72%          -91%
Encode            757/s              37%           --         -61%          -87%
:utf8_strict     1960/s             254%         159%           --          -67%
Unicode::UTF8    5915/s             970%         682%         202%            --


lv.txt: Size: 138397 Code points: 127160 (U+0000..U+007F: 117031 U+0080..U+07FF: 9021 U+0800..U+FFFF: 1108)
                   Rate :encoding(UTF-8)       Encode :utf8_strict Unicode::UTF8
:encoding(UTF-8)  605/s               --         -19%         -80%          -91%
Encode            746/s              23%           --         -75%          -88%
:utf8_strict     3043/s             403%         308%           --          -53%
Unicode::UTF8    6453/s             967%         765%         112%            --


ru.txt: Size: 151633 Code points: 85266 (U+0000..U+007F: 19263 U+0080..U+07FF: 65639 U+0800..U+FFFF: 364)
                   Rate :encoding(UTF-8)       Encode :utf8_strict Unicode::UTF8
:encoding(UTF-8)  542/s               --         -19%         -73%          -86%
Encode            673/s              24%           --         -66%          -83%
:utf8_strict     2001/s             269%         197%           --          -50%
Unicode::UTF8    4010/s             640%         496%         100%            --


sv.txt: Size: 96449 Code points: 92894 (U+0000..U+007F: 89510 U+0080..U+07FF: 3213 U+0800..U+FFFF: 171)
                    Rate :encoding(UTF-8)      Encode :utf8_strict Unicode::UTF8
:encoding(UTF-8)   923/s               --        -17%         -85%          -93%
Encode            1109/s              20%          --         -82%          -92%
:utf8_strict      5998/s             550%        441%           --          -56%
Unicode::UTF8    13604/s            1374%       1127%         127%            --


zh.txt: Size: 62891 Code points: 24519 (U+0000..U+007F: 5317 U+0080..U+07FF: 32 U+0800..U+FFFF: 19170)
                    Rate :encoding(UTF-8)      Encode :utf8_strict Unicode::UTF8
:encoding(UTF-8)  1630/s               --        -23%         -75%          -87%
Encode            2104/s              29%          --         -68%          -83%
:utf8_strict      6549/s             302%        211%           --          -48%
Unicode::UTF8    12630/s             675%        500%          93%            --
Run Code Online (Sandbox Code Playgroud)

  • @tchrist,不是,子警告仅适用于输出,使用readline()输入它们没有效果.utf8编码用于接受格式错误的UTF-8,例如编码的代理.试试这个:`$ perl -E'打开我的$ fh,"<:utf8",\"\ xED\xB0\x80"或者死掉; printf"U +%.4X \n",ord readline($ fh)'`,它完全静音,编码的U + DC00由Unicode标准组成. (3认同)
  • 善良亲切! (2认同)