Dear all,

while meditation over the vast amounts of time needed by Text::Overlaps to calculate some sort of longest common prefixes, I encountered a strange pattern.

A variable is instantiated using the string concatenation operator: my $str1 .= $self->sanitizeString ($input1);

Is it some sort of typo, that simply works because Perl can handle it?
Or is there some hidden speedup magic behind this kind of variable instantiation?

Speed differences seem to be in scope of measurement inaccuracies / noise:

#!perl use strict; use warnings; use 5.020; use Benchmark; my $s1 .= 'Peter geht nach Hause geht'; my $s2 = 'Peter nach Hause geht'; ## Method number one - a numeric sort sub test_concat { my $string = shift; my $s1x .= $string; return $s1x; } ## Method number two - an alphabetic sort sub test_normal { my $string = shift; my $s1x = $string; return $s1x; } ## We'll test each one, with simple labels my $count = 1000000; timethese ( $count, { 'Method One' => sub{ test_concat($s1); }, 'Method Two' => sub{ test_normal($s1); }, } ); exit(0);

Result:

Benchmark: timing 1000000 iterations of Method One, Method Two... Method One: 4 wallclock secs ( 4.13 usr + 0.00 sys = 4.13 CPU) @ 24 +2424.24/s (n=1000000) Method Two: 5 wallclock secs ( 4.39 usr + 0.00 sys = 4.39 CPU) @ 22 +7738.56/s (n=1000000)

Peeking at what Perl does when instantiating a variable one way or another:

>perl -MO=Concise script-concat.pl 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 4 script-concat.pl:7) v:*,&,{,x*,x&,x$ ,$,201328640 ->3 5 <2> concat[t2] vKS/2 ->6 3 <0> padsv[$s1:4,5] sRM/LVINTRO ->4 4 <$> const[PV "Peter geht nach Hause geht"] s ->5 script-concat.pl syntax OK >perl -MO=Concise script-assign.pl e-variable-instantiation.pl 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 4 script-assign.pl:7) v:*,&,{,x*,x&,x$ ,$,201328640 ->3 5 <2> sassign vKS/2 ->6 3 <$> const[PV "Peter geht nach Hause geht"] s ->4 4 <0> padsv[$s1:4,5] sRM*/LVINTRO ->5 script-assign.pl syntax OK
there is a tiny difference (concat vs. sassign).

Is it worth to put up a patch to change the operator? Or can it be neglected?


In reply to Typo or on purpose? Variable instantiation with string concatenation operator by capfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.