Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://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);

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":



  • 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-18 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found