I generally prefer to break things into more verbose code to make life easier 2 years down the road (or next week)

How is trying to understand 50 instructions easier than understanding 5. Your code is so complex it would take quite some time for me to understand it now, in weeks, in years.

experience said my approach should be faster since more but easier (for Perl) regexes are typically faster than one complex one.

Both of your regex are more complex than mine. (All three read linearly, but yours are longer.) Plus you have numerous additional Perl ops. It makes no sense for your code to be faster when the regex are executed. And it's not. You probably didn't take into account that your solution modifies the input.

('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% --

In the case where the string is shorter than 100, having the check makes it faster. You can always add that to mine. That's what ikegami2 is.

('x' x 99) Rate ikegami1 stevenmay ikegami2 ikegami1 771011/s -- -81% -83% stevenmay 4071762/s 428% -- -12% ikegami2 4620312/s 499% 13% --

Benchmark code:

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); }

In reply to Re^6: substr question by ikegami
in thread substr question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.