How can I optimize Perl code that checks for directory existence?
sub DirectoryExists {
my $param = shift;
# Remove first element of the array
shift @{$param};
# Loop through each directory to see if it exists
foreach my $directory (@{$param}) {
unless (-e $directory && -d $开发者_如何学运维directory) {
return 0;
}
}
# True
return 1;
}
Is there any way to optimize this code?
Is there any good way to optimize this code?
That algorithm is pretty efficient, because it stops at the first item but you might want to give List::Util::first
a try.
use List::Util qw<first>;
#...
return defined first { -e && -d } @$param;
The only major optimization would be that it runs in the C-layer. It's also a pretty recognizable idiom in Perl, and so despite the golf look, the purpose is to "speak perl", not to golf.
List::MoreUtils::any
would give you a similar effect and as well, it's a better fit to what you're trying to express: you're asking if any in the array are directories. (a hint though, stack parameter passing is slightly to significantly faster than constructing a reference and passing it--at least in my tests.)
Anyway, here's what it looks like:
return any { -e && -d } @$param;
Means to return true if any satisfy that expression. any
often runs in the C-layer, if the module could load its XS version. Otherwise it's "Pure Perl" and probably runs similar to yours.
However, I'm pretty sure you don't have to test for both existence and directory. I'm pretty sure that if the file does not exist, it's not going to be seen as a directory. So, you could collapse it to one condition.
I would write that code as:
sub all_directories_exist {
my $param = shift;
# Remove first element of the array
shift @{$param};
for my $dir ( @{ $param } ) {
return unless -e $directory;
return unless -d _;
}
return 1;
}
I am guessing —although I haven't benchmarked it— one cannot get much faster than that.
Two points:
Do NOT return
0
to indicate failure. You will be surprised if your sub is called in list context.Are you sure you want to modify the array pointed to by
$param
?
精彩评论