('x' x 90).' '.('x' x 10) Rate stevenmay ikegami2 ikegami1 stevenmay 256955/s -- -35% -48% ikegami2 394568/s 54% -- -19% ikegami1 490119/s 91% 24% -- ('x' x 10).' '.('x' x 90) Rate stevenmay ikegami2 ikegami1 stevenmay 102399/s -- -13% -19% ikegami2 118154/s 15% -- -7% ikegami1 126866/s 24% 7% -- #### ('x' x 99) Rate ikegami1 stevenmay ikegami2 ikegami1 771011/s -- -81% -83% stevenmay 4071762/s 428% -- -12% ikegami2 4620312/s 499% 13% -- #### use strict; use warnings; use Benchmark qw( cmpthese ); my %tests = ( stevenmay => <<'__EOI__', my $string = $input; if ( $string and length $string > 100 ){ $string = substr( $string, 0, 100); my ($tmp) = $string =~ /(.+)\s.*?$/s; # last space if possible $tmp or ($tmp) = $string =~ /(.+)\W.*?$/s; # bust on last non-word $tmp and $string = $tmp; } __EOI__ ikegami1 => <<'__EOI__', my ($string) = $input =~ /^(.{0,100})(?!\S)/s; __EOI__ ikegami2 => <<'__EOI__', my $string = $input; if (length($string) > 100) { $string =~ s/^(.{0,100})(?!\S)\K.*//s } __EOI__ ); $_ = 'use strict; use warnings; our $input; '.$_.' 1' for values(%tests); { local our $input = ('x' x 90).' '.('x' x 10); cmpthese(-1, \%tests); } { local our $input = ('x' x 10).' '.('x' x 90); cmpthese(-1, \%tests); } { local our $input = ('x' x 99); cmpthese(-1, \%tests); }