Re^3: Assigning default values to function arguments which may be �empty�
by BrowserUk (Patriarch) on Aug 19, 2016 at 09:22 UTC
|
Why would you add a completely unnecessary dependency to do something more clearly and simply done using the base language; and pay a 100%+ performance penalty for the privilege?
#! perl -slw
use strict;
use Benchmark qw[ cmpthese ];
sub f1 { # ( [$:arg] )
use 5.007003; # perl v5.7.3; required for List::Util to be availab
+le in CORE
use List::Util qw/ first /;
my $arg = first { defined && length } ( shift, 'default_value' );
$arg;
}
sub f2 {
my $arg = defined $_[0] && length $_[0] ? shift : 'default';
$arg;
}
our @args = ( undef, '', 'fred', 1, 1.5 ) x 1000;
cmpthese -1, {
stupid => q[ f1( $_ ) for @args ],
simple => q[ f2( $_ ) for @args ],
}
__END__
C:\test>junk0
Rate stupid simple
stupid 130/s -- -56%
simple 294/s 127% --
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
a) are the -s and -l switches necessary?
b) for completeness' sake:
sub f0 {
my ($arg) = grep length, shift, 'default';
$arg;
}
gave me
Rate stupid golf simple
stupid 250/s -- -13% -39%
golf 286/s 15% -- -30%
simple 411/s 65% 44% --
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
|
|
| [reply] |
|
|
|
|
|
|
|
|
|
|
@BrowserUk,
Well, from your choice of function names, I see you have a strong negative opinion about the choice. I, respectfully, disagree.
Firstly, your f2() solution is great and on target. But, I'm a language polyglot, using perl to a moderate extent... not an expert, by far. And to me, the f1() version using List::Util is easier to read and understand at a glance. I also like that it doesn't have an extra visible temporary variable, not for performance reasons, but to minimize the chance for error when reusing the idiom.
Your performance argument, while well proven and valid from the viewpoint of that single line, doesn't hold up when looking at the use of the function or script as a whole. It's a premature optimization. There is literally no perceptible (or even measurable) difference in the script execution performance between any of the solutions presented. It's fun to analyze and debate. But, until I see some sort of real performance bottleneck by the function using this idiom during some actual use, I'm leaning to the side of easier comprehension (again, for me).
But, thank you, for applying your brain power to this... as always, I enjoy reading and learning from your comments.
| [reply] [d/l] [select] |
Re^3: Assigning default values to function arguments which may be “empty”
by hippo (Archbishop) on Aug 19, 2016 at 08:58 UTC
|
use 5.007003; # perl v5.7.3; required for List::Util to be available in CORE
I must be missing something but I fail to see the advantage of this. You are deliberately disallowing users with perl 5.7.0 (say) the opportunity to run this script even if they have List::Util installed. Why would you do that?
| [reply] [d/l] |
|
|
| [reply] |
|
|
There are actually 2 problems with this approach. The first is the one I mentioned above where a perhaps pointless error is thrown for users with older perls. The second is that just because a module is in core for a certain perl version does not guarantee that it is installed. RHEL does this a lot, for example: if I were to run your code on a stock RHEL5 machine it would pass the version test (5.8.8) but then throw the compiler error because the module isn't actually installed.
FWIW, I think it would be considered fine to use a module which has been in core that long anyway. Folks running really old perls or those without core modules present will mostly be used to installing such dependencies. Your choice, of course.
| [reply] |
|
|
|
|
|
|