capfan has asked for the wisdom of the Perl Monks concerning the following question:
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:
there is a tiny difference (concat vs. sassign).>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
Is it worth to put up a patch to change the operator? Or can it be neglected?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Typo or on purpose? Variable instantiation with string concatenation operator
by ikegami (Patriarch) on Oct 06, 2015 at 15:23 UTC | |
by Crackers2 (Parson) on Oct 06, 2015 at 18:07 UTC | |
by ikegami (Patriarch) on Oct 07, 2015 at 19:37 UTC | |
Re: Typo or on purpose? Variable instantiation with string concatenation operator
by kennethk (Abbot) on Oct 06, 2015 at 15:41 UTC | |
by capfan (Sexton) on Oct 06, 2015 at 20:46 UTC |