Good point. I'll see if I can set up some benchmarking and post the results.
Update:
Running the code below under ubuntu bash under windows with LC_NUMERIC set to de_DE.iso88591 indicates that s///r is about 20% faster than y//r. Both are in the order of millions per second, though.
And as yet another update, the order does switch between runs so it's not a consistent outcome.
Results:
de_DE.iso88591
3,1
3.1
3.1
Rate y_sprintf s_sprintf sprintf
y_sprintf 4646523/s -- -19% -63%
s_sprintf 5721423/s 23% -- -54%
sprintf 12396265/s 167% 117% --
Code:
use Benchmark qw {:all};
use 5.016;
local $| = 1;
use POSIX qw /locale_h/;
use locale;
print POSIX::setlocale(LC_NUMERIC) . "\n";
my $x;
$x = sprintf "%.2g", 3.14;
print "$x\n";
$x = sprintf ("%.2g", 3.14) =~ y/,/./r;
print "$x\n";
$x = sprintf ("%.2g", 3.14) =~ s/,/./r;
print "$x\n";
cmpthese (
-3,
{
sprintf => 'use strict; use warnings; my $x = sprintf ("%.2g
+", 3.14)',
y_sprintf => 'use strict; use warnings; my $x = sprintf ("%.2g
+", 3.14) =~ y/,/./r',
s_sprintf => 'use strict; use warnings; my $x = sprintf ("%.2g
+", 3.14) =~ s/,/./r',
}
);
(post was further updated to distinguish results from code)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.