in reply to Short URL?
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... ;-)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% --
|
|---|