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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |