The first regular expression is quite neat. I came up with a similar - but quite ungainly - solution to the problem (see the code below). I assumed that my code, being a bit of a mess would be particularly slow and inefficient so being curious I compared the two solutions using some code borrowed from
httptech.
#!/usr/local/bin/perl
use Benchmark;
my $str = "the %0A quick %2b brown %63 fox %63 jumped %24 over %7e th
+e %4f%45 lazy %25 dog";
timethese (500000, {
'myway' => sub {
$_ = $str;
while (/%[0-9A-Fa-f]{2}/) {
$_ = $&;
/[0-9A-Fa-f]{2}/;
$ob[0] = hex($&);
$temp = pack("C*", @ob);
$name =~ s/%[0-9A-Fa-f]{2}/$temp/;
$_ = $name;
}
},
'regexpway' => sub {
$_ = $str;
s/%([a-fA-F0-9]{2})/pack("C", hex($1))/eg;
},
});
Benchmark: timing 500000 iterations of myway, regexpway...
myway: 23 wallclock secs (21.99 usr + 0.00 sys = 21.99 CPU)
regexpway: 57 wallclock secs (57.58 usr + 0.00 sys = 57.58 CPU)
I was quite suprised that my messy solution appeared to be significantly faster than the reg exp.
Any ideas why this is?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.