Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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);


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 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 4 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 syntax OK >perl -MO=Concise 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 4 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 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

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

  • 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.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2022-11-27 08:54 GMT
Find Nodes?
    Voting Booth?