Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Typo or on purpose? Variable instantiation with string concatenation operator

by kennethk (Abbot)
on Oct 06, 2015 at 15:41 UTC ( #1143940=note: print w/replies, xml ) Need Help??


in reply to Typo or on purpose? Variable instantiation with string concatenation operator

An undefined value is converted to an empty string in string context. So the two are functionally equivalent, even if one is potentially confusing.

The benchmark you wrote is not a good test because the concatenation time will be lost in the overhead for the function call. To some degree, you could argue that means, by definition, that it doesn't matter which you pick; however, if you are going to test, you should test the right thing.

#!perl use strict; use warnings; 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; for (1 .. 100000) { $s1x .= $string; undef $s1x; } return $s1x; } ## Method number two - an alphabetic sort sub test_normal { my $string = shift; my $s1x; for (1 .. 100000) { $s1x = $string; undef $s1x; } return $s1x; } ## We'll test each one, with simple labels my $count = 100; timethese ( $count, { 'Method One' => sub{ test_concat($s1); }, 'Method Two' => sub{ test_normal($s1); }, } ); exit(0);
which yields
Benchmark: timing 100 iterations of Method One, Method Two... Method One: 4 wallclock secs ( 3.40 usr + 0.00 sys = 3.40 CPU) @ 29 +.40/s (n=100) Method Two: 3 wallclock secs ( 3.59 usr + 0.00 sys = 3.59 CPU) @ 27 +.87/s (n=100)
or
Benchmark: timing 100 iterations of Method One, Method Two... Method One: 3 wallclock secs ( 3.37 usr + 0.00 sys = 3.37 CPU) @ 29 +.68/s (n=100) Method Two: 4 wallclock secs ( 3.49 usr + 0.00 sys = 3.49 CPU) @ 28 +.61/s (n=100)
or
Benchmark: timing 100 iterations of Method One, Method Two... Method One: 4 wallclock secs ( 3.45 usr + 0.00 sys = 3.45 CPU) @ 29 +.00/s (n=100) Method Two: 3 wallclock secs ( 3.49 usr + 0.00 sys = 3.49 CPU) @ 28 +.62/s (n=100)
So I would conclude that skipping the no-op would save me on the order 1 second for every 10 million assignments. Whether this matters for your use case depends on your use case. It would not matter for any of mine.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Typo or on purpose? Variable instantiation with string concatenation operator
by capfan (Sexton) on Oct 06, 2015 at 20:46 UTC

    Thank you for the insights. In this case, I personally will "correct" it in my code. When I read the code next time, I don't have to think about this particular line of code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1143940]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-11-30 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?