First time I've done a benchmark, but I think I did it right...
use Benchmark qw(timethese cmpthese); $url = 'http://www.larryandnatalie.com/isapi.dll/c/content/f/viewprope +rty/siteid/wb5KAM/contentclass/PICT/contentid/ZZZZZZVR/propertyname/O +riginal/~/yamaha_58_page_11_jpg.jpg'; $r = timethese( -5, { 'regex' => sub{$url =~ s/^(.{35})(?=.{19}).+(.{15})$/$1...$2/;}, 'substr' => sub{$url = substr($linktext, 0, 35) . "..." . substr($ +linktext, -15) if length($linktext) > 53;}, }); cmpthese $r; __END__ Benchmark: running regex, substr for at least 5 CPU seconds... regex: 2 wallclock secs ( 5.07 usr + -0.07 sys = 5.00 CPU) @ 63 +3939.80/s (n=3169699) substr: 6 wallclock secs ( 4.69 usr + 0.34 sys = 5.03 CPU) @ 35 +32631.81/s (n=17769138) Rate regex substr regex 633940/s -- -82% substr 3532632/s 457% --
It would seem as though substr is faster for doing it many times, but the regex for doing it once. Don't quote me on that though, it's quite likely that I've interpreted the results wrong... ;-)

In reply to Re: Short URL? by The Mad Hatter
in thread Short URL? by bobafifi

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.