develooper Front page | perl.perl5.porters | Postings from July 2021

Quadratic time constant folding

Thread Next
From:
Ovid via perl5-porters
Date:
July 20, 2021 16:28
Subject:
Quadratic time constant folding
Message ID:
2050564555.524268.1626798520562@mail.yahoo.com
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
Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About