#!/usr/bin/env perl -l use strict; use warnings; use constant STRING => " \t aaa bbb ccc \t \n"; use Benchmark 'cmpthese'; print 'Sanity Tests:'; print 'shoura: >', shoura_code(), '<'; print 'kcott: >', kcott_code(), '<'; print 'marshall: >', marshall_code(), '<'; cmpthese 0 => { S => \&shoura_code, K => \&kcott_code, M => \&marshall_code, }; sub shoura_code { local $_ = STRING; chomp; s/^\s+|\s+$//g; return $_; } sub kcott_code { local $_ = STRING; ($_) = /^\s*(.*?)\s*$/; return $_; } sub marshall_code { local $_ = STRING; s/^\s+//; s/\s+$//; return $_; } #### Sanity Tests: shoura: >aaa bbb ccc< kcott: >aaa bbb ccc< marshall: >aaa bbb ccc< Rate S M K S 292306/s -- -32% -37% M 432626/s 48% -- -7% K 464863/s 59% 7% -- #### #!/usr/bin/env perl -l use strict; use warnings; use Benchmark 'cmpthese'; my %seq = ( 'W X Y' => 'WbbbXbbbY', 'X Y' => 'XbbbY', 'X Y Z' => 'XbbbYbbbZ', ); print 'Sanity Tests:'; print 'kcott: >', kcott_code(), '<'; print 'marshall: >', marshall_code(), '<'; cmpthese 0 => { K => \&kcott_code, M => \&marshall_code, }; sub kcott_code { my $re = qr{(@{[join '|', sort { length $b <=> length $a } keys %seq]})}; return $re; } sub marshall_code { my $search_phrases = join '|', sort { length $b <=> length $a } keys %seq; my $re = qr{($search_phrases)}; return $re; } #### Sanity Tests: kcott: >(?^:(W X Y|X Y Z|X Y))< marshall: >(?^:(W X Y|X Y Z|X Y))< Rate K M K 240417/s -- -13% M 277468/s 15% -- #### my %seq = ( 'W X Y' => 'WbbbXbbbY', 'X Y' => 'XbbbY', 'X Y Z' => 'XbbbYbbbZ', ); #### W XbbbY Z WbbbXbbbY Z W XbbbYbbbZ #### sort { length $b <=> length $a || $a cmp $b }