develooper Front page | perl.perl5.porters | Postings from August 2013

Re: [perl #117355] [lu]cfirst don't respect 'use bytes'

Thread Previous | Thread Next
From:
Brian Fraser
Date:
August 12, 2013 21:01
Subject:
Re: [perl #117355] [lu]cfirst don't respect 'use bytes'
Message ID:
CA+nL+napV6hKGANkj4pDVy5ry93jwTgM9J2BEAyYUJf7ST5hXw@mail.gmail.com
On Mon, Aug 12, 2013 at 5:08 PM, Victor Efimov via RT <
perlbug-followup@perl.org> wrote:

> On Sun Aug 11 19:56:53 2013, rjbs wrote:
> > I haven't given this a lot of thought, but I think that if we can make
> > bytes.pm ejectable, we
> > should do so.  It's okay if it gets slower, since we've been telling
> > people for years that it's only a
> > debugging tool, if that.
> >
> > Thoughts? Objections?
>
>
> Possible use bytes to speedup regexps by 20-40% at some cases:
>
> use warnings;
> use strict;
> use utf8;
> use bytes ();
> use Encode;
> use Benchmark qw/:all/;
>
> sub test
> {
>   my ($s) = @_;
>   $s = ($s x 400)."z";
>   $s =~ /z/ for (1..100);
> }
>
> sub try_drop_utf8_flag
> {
>   Encode::_utf8_off($_[0]) if utf8::is_utf8($_[0]) &&
> (bytes::length($_[0]) == length($_[0]));
> }
>
> my $ascii = "x";
> my ($ascii_u, undef) = split(/ /, "x ั‚ะตัั‚");
>
> die unless $ascii_u eq $ascii;
>
> cmpthese(-1,{
>   'ascii' => sub { test($ascii); },
>   'ascii with utf8 on' => sub { test($ascii_u);},
>   'ascii with utf8 bit cleared' => sub {
>     my $s = $ascii_u;
>     try_drop_utf8_flag($s);
>     die if utf8::is_utf8($s);
>     test($s);
>    },
> });
>
> __END__
>
> perl-5.18.0
> ==========
>                                Rate ascii with utf8 on ascii with utf8
> bit cleared ascii
> ascii with utf8 on          22635/s                 --
>       -27%  -28%
> ascii with utf8 bit cleared 30919/s                37%
>         --   -2%
> ascii                       31508/s                39%
>         2%    --
>
> perl-5.10.0
> ==========
>                                Rate ascii with utf8 on ascii with utf8
> bit cleared ascii
> ascii with utf8 on          25831/s                 --
>       -19%  -21%
> ascii with utf8 bit cleared 31717/s                23%
>         --   -4%
> ascii                       32881/s                27%
>         4%    --
>
>
I've tweaked your benchmark a bit:
use warnings;
use strict;
use utf8;
use Benchmark qw/:all/;

my $ascii   = ("x" x 400) . "z";
my $ascii_u = ("\N{LATIN SMALL LETTER X}" x 400) . "z";
my $bytes   = $ascii_u;

utf8::encode($bytes);

die unless $ascii_u eq $ascii;

cmpthese(timethese(10000,{
  'ascii'                    => sub { $ascii   =~ /z/   for 1..100 },
  'ascii, utf8'              => sub { $ascii_u =~ /z/   for 1..100 },
  'ascii, utf8 cleared'      => sub { $bytes   =~ /z/   for 1..100 },
  'ascii, /aa'               => sub { $ascii   =~ /z/aa for 1..100 },
  'ascii, utf8, /aa'         => sub { $ascii_u =~ /z/aa for 1..100 },
  'ascii, utf8 cleared, /aa' => sub { $bytes   =~ /z/aa for 1..100 },
  'ascii, bytes'             => sub { use bytes; $ascii =~ /z/ for 1..100 },
  'ascii, utf8, bytes'       => sub { use bytes; $ascii_u =~ /z/ for 1..100
},
  'ascii, utf8 cleared, bytes' => sub { use bytes; $bytes =~ /z/ for 1..100
},
}));

Which gives me, on 5.14.2:
Benchmark: timing 10000 iterations of ascii, ascii, /aa, ascii, bytes,
ascii, utf8, ascii, utf8 cleared, ascii, utf8 cleared, /aa, ascii, utf8
cleared, bytes, ascii, utf8, /aa, ascii, utf8, bytes...
     ascii:  2 wallclock secs ( 1.55 usr +  0.00 sys =  1.55 CPU) @
6451.61/s (n=10000)
ascii, /aa:  1 wallclock secs ( 1.54 usr +  0.00 sys =  1.54 CPU) @
6493.51/s (n=10000)
ascii, bytes:  2 wallclock secs ( 1.71 usr +  0.00 sys =  1.71 CPU) @
5847.95/s (n=10000)
ascii, utf8:  3 wallclock secs ( 3.04 usr +  0.00 sys =  3.04 CPU) @
3289.47/s (n=10000)
ascii, utf8 cleared:  2 wallclock secs ( 1.87 usr +  0.00 sys =  1.87 CPU)
@ 5347.59/s (n=10000)
ascii, utf8 cleared, /aa:  2 wallclock secs ( 1.84 usr +  0.00 sys =  1.84
CPU) @ 5434.78/s (n=10000)
ascii, utf8 cleared, bytes:  2 wallclock secs ( 1.92 usr +  0.00 sys =
1.92 CPU) @ 5208.33/s (n=10000)
ascii, utf8, /aa:  3 wallclock secs ( 2.87 usr +  0.00 sys =  2.87 CPU) @
3484.32/s (n=10000)
ascii, utf8, bytes:  2 wallclock secs ( 1.82 usr +  0.01 sys =  1.83 CPU) @
5464.48/s (n=10000)
                             Rate ascii, utf8 ascii, utf8, /aa ascii, utf8
cleared, bytes ascii, utf8 cleared ascii, utf8 cleared, /aa ascii, utf8,
bytes ascii, bytes ascii ascii, /aa
ascii, utf8                3289/s          --
-6%                       -37%                -38%
-39%               -40%         -44%  -49%       -49%
ascii, utf8, /aa           3484/s          6%
--                       -33%                -35%
-36%               -36%         -40%  -46%       -46%
ascii, utf8 cleared, bytes 5208/s         58%
49%                         --                 -3%
-4%                -5%         -11%  -19%       -20%
ascii, utf8 cleared        5348/s         63%
53%                         3%                  --
-2%                -2%          -9%  -17%       -18%
ascii, utf8 cleared, /aa   5435/s         65%
56%                         4%                  2%
--                -1%          -7%  -16%       -16%
ascii, utf8, bytes         5464/s         66%
57%                         5%                  2%
1%                 --          -7%  -15%       -16%
ascii, bytes               5848/s         78%
68%                        12%                  9%
8%                 7%           --   -9%       -10%
ascii                      6452/s         96%
85%                        24%                 21%
19%                18%          10%    --        -1%
ascii, /aa                 6494/s         97%
86%                        25%                 21%
19%                19%          11%    1%         --

On 5.16.2:
Benchmark: timing 10000 iterations of ascii, ascii, /aa, ascii, bytes,
ascii, utf8, ascii, utf8 cleared, ascii, utf8 cleared, /aa, ascii, utf8
cleared, bytes, ascii, utf8, /aa, ascii, utf8, bytes...
     ascii:  2 wallclock secs ( 1.51 usr +  0.00 sys =  1.51 CPU) @
6622.52/s (n=10000)
ascii, /aa:  1 wallclock secs ( 1.54 usr +  0.00 sys =  1.54 CPU) @
6493.51/s (n=10000)
ascii, bytes:  2 wallclock secs ( 1.53 usr +  0.00 sys =  1.53 CPU) @
6535.95/s (n=10000)
ascii, utf8:  2 wallclock secs ( 2.11 usr +  0.00 sys =  2.11 CPU) @
4739.34/s (n=10000)
ascii, utf8 cleared:  1 wallclock secs ( 1.52 usr +  0.00 sys =  1.52 CPU)
@ 6578.95/s (n=10000)
ascii, utf8 cleared, /aa:  2 wallclock secs ( 1.52 usr +  0.01 sys =  1.53
CPU) @ 6535.95/s (n=10000)
ascii, utf8 cleared, bytes:  2 wallclock secs ( 1.51 usr +  0.01 sys =
1.52 CPU) @ 6578.95/s (n=10000)
ascii, utf8, /aa:  2 wallclock secs ( 2.11 usr +  0.00 sys =  2.11 CPU) @
4739.34/s (n=10000)
ascii, utf8, bytes:  1 wallclock secs ( 1.56 usr +  0.00 sys =  1.56 CPU) @
6410.26/s (n=10000)
                             Rate ascii, utf8 ascii, utf8, /aa ascii, utf8,
bytes ascii, /aa ascii, bytes ascii, utf8 cleared, /aa ascii, utf8 cleared,
bytes ascii, utf8 cleared ascii
ascii, utf8                4739/s          --
-0%               -26%       -27%         -27%
-27%                       -28%                -28%  -28%
ascii, utf8, /aa           4739/s          0%
--               -26%       -27%         -27%
-27%                       -28%                -28%  -28%
ascii, utf8, bytes         6410/s         35%
35%                 --        -1%          -2%
-2%                        -3%                 -3%   -3%
ascii, /aa                 6494/s         37%
37%                 1%         --          -1%
-1%                        -1%                 -1%   -2%
ascii, bytes               6536/s         38%
38%                 2%         1%           --
-0%                        -1%                 -1%   -1%
ascii, utf8 cleared, /aa   6536/s         38%
38%                 2%         1%           0%
--                        -1%                 -1%   -1%
ascii, utf8 cleared, bytes 6579/s         39%
39%                 3%         1%           1%
1%                         --                  0%   -1%
ascii, utf8 cleared        6579/s         39%
39%                 3%         1%           1%
1%                         0%                  --   -1%
ascii                      6623/s         40%
40%                 3%         2%           1%
1%                         1%                  1%    --

(skipping 5.18 because I compiled it with different options than everything
else, whoops)
On blead:
Benchmark: timing 10000 iterations of ascii, ascii, /aa, ascii, bytes,
ascii, utf8, ascii, utf8 cleared, ascii, utf8 cleared, /aa, ascii, utf8
cleared, bytes, ascii, utf8, /aa, ascii, utf8, bytes...
     ascii:  2 wallclock secs ( 1.60 usr +  0.00 sys =  1.60 CPU) @
6250.00/s (n=10000)
ascii, /aa:  1 wallclock secs ( 1.58 usr +  0.00 sys =  1.58 CPU) @
6329.11/s (n=10000)
ascii, bytes:  2 wallclock secs ( 1.59 usr +  0.00 sys =  1.59 CPU) @
6289.31/s (n=10000)
ascii, utf8:  2 wallclock secs ( 1.63 usr +  0.00 sys =  1.63 CPU) @
6134.97/s (n=10000)
ascii, utf8 cleared:  1 wallclock secs ( 1.61 usr +  0.00 sys =  1.61 CPU)
@ 6211.18/s (n=10000)
ascii, utf8 cleared, /aa:  2 wallclock secs ( 1.64 usr +  0.00 sys =  1.64
CPU) @ 6097.56/s (n=10000)
ascii, utf8 cleared, bytes:  2 wallclock secs ( 1.63 usr +  0.00 sys =
1.63 CPU) @ 6134.97/s (n=10000)
ascii, utf8, /aa:  1 wallclock secs ( 1.63 usr +  0.00 sys =  1.63 CPU) @
6134.97/s (n=10000)
ascii, utf8, bytes:  2 wallclock secs ( 1.63 usr +  0.00 sys =  1.63 CPU) @
6134.97/s (n=10000)
                             Rate ascii, utf8 cleared, /aa ascii, utf8,
bytes ascii, utf8, /aa ascii, utf8 ascii, utf8 cleared, bytes ascii, utf8
cleared ascii ascii, bytes ascii, /aa
ascii, utf8 cleared, /aa   6098/s                       --
-1%              -1%         -1%                        -1%
-2%   -2%          -3%        -4%
ascii, utf8, bytes         6135/s                       1%
--              -0%         -0%                        -0%
-1%   -2%          -2%        -3%
ascii, utf8, /aa           6135/s                       1%
-0%               --         -0%                        -0%
-1%   -2%          -2%        -3%
ascii, utf8                6135/s                       1%
0%               0%          --                        -0%
-1%   -2%          -2%        -3%
ascii, utf8 cleared, bytes 6135/s                       1%
0%               0%          0%                         --
-1%   -2%          -2%        -3%
ascii, utf8 cleared        6211/s                       2%
1%               1%          1%                         1%
--   -1%          -1%        -2%
ascii                      6250/s                       2%
2%               2%          2%                         2%
1%    --          -1%        -1%
ascii, bytes               6289/s                       3%
3%               3%          3%                         3%
1%    1%           --        -1%
ascii, /aa                 6329/s                       4%
3%               3%          3%                         3%
2%    1%           1%         --

So, your argument about the speedup might hold some water on older perls,
but the way to get it is not necessarily "use bytes", but "if you don't
want unicode semantics, encode your strings before matching" -- and as of
blead, looks like ascii+utf8 now matches just as fast as plain ascii.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About