note
swl
<p>Good point. I'll see if I can set up some benchmarking and post the results. </p>
<p>Update:</p>
<p>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. </p>
<p>And as yet another update, the order does switch between runs so it's not a consistent outcome.</p>
<p>Results:</p>
<c>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% --
</c>
<p>Code:</p>
<c>
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',
}
);
</c>
<p>(post was further updated to distinguish results from code)</p>
11119927
11119934