Update: i added petral's and impossiblerobot's routines to the comparison...

although my solution was complex and used regex mojo, chipmunk's is by far the faster of the two. impossiblerobot's first is fastest of all, becase there's no real thinking involved. consider

#!/usr/local/bin/perl -w use strict; use Benchmark; my $s = '00a0c801adc6'; timethese(100000,{ particle => sub { my $str = $s; $str =~ s/([\w]{2})(?!$)/$1:/gio; }, chipmunk => sub { my $str = $s; $str = join(':', $str =~ /../g); }, petral => sub { my $str = $s; $str = join(':', split /(?=(?:..)+$)/,$str); }, robot1 => sub { my $str = $s; $str = join(':', unpack 'a2a2a2a2a2a2', $str); }, robot2 => sub { my $str = $s; $str = join(':', unpack('a2' x (length($str)/2), $str)); }, });
which results, on my 450MHz, 768MB ram, win98 box, in:
C:\WINDOWS\Desktop>perl test_mac.pl Benchmark: timing 100000 iterations of chipmunk, particle, petral, rob +ot1, robot2... chipmunk: 3 wallclock secs ( 2.52 usr + 0.00 sys = 2.52 CPU) @ 396 +82.54/s (n=100000) particle: 6 wallclock secs ( 6.48 usr + 0.00 sys = 6.48 CPU) @ 154 +32.10/s (n=100000) petral: 5 wallclock secs ( 6.05 usr + 0.00 sys = 6.05 CPU) @ 165 +28.93/s (n=100000) robot1: 2 wallclock secs ( 1.27 usr + 0.00 sys = 1.27 CPU) @ 787 +40.16/s (n=100000) robot2: 3 wallclock secs ( 3.02 usr + 0.00 sys = 3.02 CPU) @ 331 +12.58/s (n=100000)
this goes to show you just how much more time regexes can add to your algorithm.

~Particle


In reply to Re: More Help with Regex by particle
in thread More Help with Regex by lucky1

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.