For you merlyn, I made it run on a string. Plus, I ran it on a faster machine since mine is busy.
use strict; use Benchmark; for ( 1..5 ) # Do five tests. { $_ = int( rand(10_000) ) ** int( rand(3) + 2 ); $_ = "For $_ Merlyn " . reverse($_) . " plus the constants ". "8634641234541275032000523 and 8,634,641,234,541,275,032,000, +523"; print $_, "\n"; timethese( 1_000_000, { 'KM' => sub { 1 while s/^(-?\d+)(\d{3})/$1,$2/ }, 'Merlyn' => sub {s/(\d)(?=(\d{3})+(\D|$))/$1\,/g} }); print "\n", "- " x 39, "-\n"; }
Output:
For 2699449 Merlyn 9449962 plus the constants 8634641234541275032000523 and 8,63
4,641,234,541,275,032,000,523
Benchmark: timing 1000000 iterations of KM, Merlyn...
        KM:  1 wallclock secs ( 1.04 usr +  0.00 sys =  1.04 CPU) @ 960614.79/s
(n=1000000)
    Merlyn:  0 wallclock secs ( 0.51 usr +  0.00 sys =  0.51 CPU) @ 1956947.16/s
 (n=1000000)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
For 78836641 Merlyn 14663887 plus the constants 8634641234541275032000523 and 8,
634,641,234,541,275,032,000,523
Benchmark: timing 1000000 iterations of KM, Merlyn...
        KM:  1 wallclock secs ( 1.06 usr +  0.00 sys =  1.06 CPU) @ 942507.07/s
(n=1000000)
    Merlyn:  0 wallclock secs ( 0.58 usr +  0.00 sys =  0.58 CPU) @ 1721170.40/s
 (n=1000000)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
For 126128378375 Merlyn 573873821621 plus the constants 863464123454127503200052
3 and 8,634,641,234,541,275,032,000,523
Benchmark: timing 1000000 iterations of KM, Merlyn...
        KM:  2 wallclock secs ( 1.02 usr +  0.00 sys =  1.02 CPU) @ 979431.93/s
(n=1000000)
    Merlyn:  1 wallclock secs ( 0.53 usr +  0.00 sys =  0.53 CPU) @ 1883239.17/s
 (n=1000000)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
For 8665653464 Merlyn 4643565668 plus the constants 8634641234541275032000523 an
d 8,634,641,234,541,275,032,000,523
Benchmark: timing 1000000 iterations of KM, Merlyn...
        KM:  2 wallclock secs ( 1.12 usr +  0.00 sys =  1.12 CPU) @ 891265.60/s
(n=1000000)
    Merlyn:  0 wallclock secs ( 0.53 usr +  0.00 sys =  0.53 CPU) @ 1886792.45/s
 (n=1000000)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
For 36300625 Merlyn 52600363 plus the constants 8634641234541275032000523 and 8,
634,641,234,541,275,032,000,523
Benchmark: timing 1000000 iterations of KM, Merlyn...
        KM:  2 wallclock secs ( 0.98 usr +  0.00 sys =  0.98 CPU) @ 1018329.94/s
 (n=1000000)
    Merlyn:  0 wallclock secs ( 0.54 usr +  0.00 sys =  0.54 CPU) @ 1848428.84/s
 (n=1000000)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

In reply to RE: RE: RE: regexp for adding commas to a number by Adam
in thread regexp for adding commas to a number by Kozz

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.