I figured this code would be executed repeatedly, so performance would matter.

For strings with very few surrounding spaces,

length=300 Rate merlyn ikegami merlyn 8132/s -- -80% ikegami 41078/s 405% -- length=500 Rate merlyn ikegami merlyn 1660/s -- -91% ikegami 18224/s 998% -- length=1000 Rate merlyn ikegami merlyn 488/s -- -92% ikegami 6095/s 1149% --

I wrote a more comprehensive benchmark, but I'm getting inconsistent results at the moment. I'll run on it a more stable machine tomorrow.

use strict; use warnings; use Benchmark qw( cmpthese ); my %tests = ( ikegami11 => 's/^(.{256,}?)\s+\z/$1/s, s/^\s*(.{256}).*\z/$1/s + if length > 256;', ikegami21 => 's/(?<=.{6})\s+\z//s, s/^\s*(.{256}).*\z/$1/s + if length > 256;', ikegami31 => 's/.{6}\K\s+\z//s, s/^\s*(.{256}).*\z/$1/s + if length > 256;', ikegami12 => 's/^(.{256,}?)\s+\z/$1/s, s/^\s+(?=.{6})//s, s/(?<=^.{ +6}).*\z//s if length > 256;', ikegami22 => 's/(?<=.{6})\s+\z//s, s/^\s+(?=.{6})//s, s/(?<=^.{ +6}).*\z//s if length > 256;', ikegami32 => 's/.{6}\K\s+\z//s, s/^\s+(?=.{6})//s, s/(?<=^.{ +6}).*\z//s if length > 256;', ikegami13 => 's/^(.{256,}?)\s+\z/$1/s, s/^\s+(?=.{6})//s, s/(?<=.{6 +}).*\z//s if length > 256;', ikegami23 => 's/(?<=.{6})\s+\z//s, s/^\s+(?=.{6})//s, s/(?<=.{6 +}).*\z//s if length > 256;', ikegami33 => 's/.{6}\K\s+\z//s, s/^\s+(?=.{6})//s, s/(?<=.{6 +}).*\z//s if length > 256;', ikegami14 => 's/^(.{256,}?)\s+\z/$1/s, s/^\s+(?=.{6})//s, s/^.{6}\K +.*\z//s if length > 256;', ikegami24 => 's/(?<=.{6})\s+\z//s, s/^\s+(?=.{6})//s, s/^.{6}\K +.*\z//s if length > 256;', ikegami34 => 's/.{6}\K\s+\z//s, s/^\s+(?=.{6})//s, s/^.{6}\K +.*\z//s if length > 256;', merlyn => 's/\s$// || s/^\s// || s/.$// while length > 256;', ); $_ = 'use strict; use warnings; local $_ = our $pat;' . $_ for values(%tests); for my $len (256, 260, 300, 500) { for our $pat ( ' ' x $len, (' ' x ($len/2)) . ('x' x ($len/2)), 'x' x $len, ) { printf("length=%u, pat=\"%s...%s\"\n", $len, substr($pat, 0, 5), + substr($pat, -5)); cmpthese(-1, \%tests); print("\n"); } }

In reply to Re^2: Truncate string to limited length, throwing away unimportant characters first. by ikegami
in thread Truncate string to limited length, throwing away unimportant characters first. by ambrus

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.