Regularni vyraz

Honza Pazdziora adelton na fi.muni.cz
Středa Únor 10 14:45:24 MET 1999


> a co tak skusit $pwd =~ s/\s*$//;

Nebo (nejlepe) $pwd =~ s/\s+$//;

Ke svemu prekvapeni jsem zjistil, ze

$ perl
use Benchmark;
timethese(100000, {
        'plus' => sub { my $a = 'asd asdf'; $a =~ s/\s+$//; },
        'star' => sub { my $a = 'asd asdf'; $a =~ s/\s*$//; },
        });
__END__
Benchmark: timing 100000 iterations of plus, star...
      plus:  0 wallclock secs ( 1.45 usr +  0.00 sys =  1.45 CPU)
      star:  2 wallclock secs ( 2.55 usr +  0.00 sys =  2.55 CPU)
$ perl
use Benchmark;
timethese(100000, {
        'plus' => sub { my $a = 'asd asdf   '; $a =~ s/\s+$//; },
        'star' => sub { my $a = 'asd asdf   '; $a =~ s/\s*$//; },
        });
__END__
Benchmark: timing 100000 iterations of plus, star...
      plus:  1 wallclock secs ( 1.79 usr +  0.00 sys =  1.79 CPU)
      star:  3 wallclock secs ( 2.62 usr +  0.00 sys =  2.62 CPU)

coz neni uplne zanedbatelne. Nejak intuitivne tusim, ze je to tim, ze
\s* uspeje vlastne mezi kazdymi dvema pismeny, to znamena, ze ma vsude
uspech a teprve pak se porovnava $, zatimco \s+ muze uspet jen na
korektni mezere, tedy pripadu, kdy se porovnava $ je mene. Proste \s*
ma vice uspechu, ktere pri nesplnenem $ vedou k backtrackovani, nez
\s+. Viz. MRE.

-- 
------------------------------------------------------------------------
 Honza Pazdziora | adelton na fi.muni.cz | http://www.fi.muni.cz/~adelton/
Get refund for operating system you don't use: www.linuxmall.com/refund/
------------------------------------------------------------------------


Další informace o konferenci Perl