Re: Super regexp to format numbers
Artur Penttinen <[email protected]> Tue, 29 Aug 2006 09:38:54 +0400
| Newsgroups | gmane.comp.lang.perl.fun |
|---|---|
| Message-ID | <[email protected]> |
29.08.06, 03:18, A. Pagaltzis <[email protected]>: > * Alexandre Jousset <[email protected]> [2006-08-28 22:55]: > > It works only for integers... > This list is “fun with Perl.” Splitting a string at the dot char > is not very fun, no? I considered that part to be obvious. and now, benchmarking: # perl -MBenchmark=timethese -de0 DB<1> $a = "21285063.14" DB<2> sub A () { local $_ = $a; 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; return $_; } DB<3> sub B () { local $_ = $a; s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g; return $_; } DB<4> sub C () { my ($aa,$bb) = split "\\.",$a; return scalar (reverse join ' ', unpack '(A3)*', reverse $aa) . ".$bb"; } DB<5> timethese (1_000_000,{'A' => \&A,'B' => \&B,'C' => \&C}) Benchmark: timing 1000000 iterations of A, B, C... A: 27 wallclock secs (17.16 usr + 0.02 sys = 17.18 CPU) @ 58207.22/s (n=1000000) B: 21 wallclock secs (14.59 usr + 0.01 sys = 14.60 CPU) @ 68493.15/s (n=1000000) C: 6 wallclock secs ( 6.12 usr + -0.02 sys = 6.10 CPU) @ 163934.43/s (n=1000000) -- wbw, artur