In case it was missed here, Aristotle posted to BPO about quadratic time constant folding: http://blogs.perl.org/users/aristotle/2021/06/stringbuilder.html. To be fair, his post was a bit obscure in that he never quite said what he was talking about, but he linked to another blog entry discussing quadratic time constant folding in other languages and how to fix it: https://accidentallyquadratic.tumblr.com/
This is the example Aristotle provided:
use Benchmark::Dumb 'cmpthese';
( $bar, $quux ) = qw( bar quux );
cmpthese( 0.0002, {
conc => q{
my $str = "xxx ";
(((( $str .= "foo" ).= $::bar ).= "baz" ).= $::quux ).= "qux";
},
intp => q{
my $str = "xxx ";
$str .= "foo${::bar}baz${::quux}qux";
},
} );
This is the output:
Rate/s Precision/s intp conc
intp 5.416e+06 19000 -- -40.5%
conc 9.097e+06 53000 68+-1.1% --
If you prefer, I converted that to the standard Benchmark module and did 10 million iterations:
Rate intp conc
intp 4000000/s -- -46%
conc 7407407/s 85% --
(If it wasn't immediately clear, those two snippets assign the same value to $str)
I don't think there's an issue here, but I thought it was worth mentioning.
Best,Ovid
-- IT consulting, training, specializing in Perl, databases, and agile developmenthttp://www.allaroundtheworld.fr/.
Buy my book! - http://bit.ly/beginning_perl
This is the example Aristotle provided:
use Benchmark::Dumb 'cmpthese';
( $bar, $quux ) = qw( bar quux );
cmpthese( 0.0002, {
conc => q{
my $str = "xxx ";
(((( $str .= "foo" ).= $::bar ).= "baz" ).= $::quux ).= "qux";
},
intp => q{
my $str = "xxx ";
$str .= "foo${::bar}baz${::quux}qux";
},
} );
This is the output:
Rate/s Precision/s intp conc
intp 5.416e+06 19000 -- -40.5%
conc 9.097e+06 53000 68+-1.1% --
If you prefer, I converted that to the standard Benchmark module and did 10 million iterations:
Rate intp conc
intp 4000000/s -- -46%
conc 7407407/s 85% --
(If it wasn't immediately clear, those two snippets assign the same value to $str)
I don't think there's an issue here, but I thought it was worth mentioning.
Best,Ovid
-- IT consulting, training, specializing in Perl, databases, and agile developmenthttp://www.allaroundtheworld.fr/.
Buy my book! - http://bit.ly/beginning_perl