C
Chief Squawtendrawpet
I find that Win32::File::GetAttributes() is faster than Perl's
built-in -d and -f file test operators, which seems counter-intuitive
to me. Did I go astray somewhere in my testing (shown below for -f)?
Is there any reason not to switch to GetAttributes() when speed is an
issue? Perhaps this is well-known, but I didn't find much in a Google
search.
Chief S.
$^W = 1;
use strict;
use Win32::File qw(GetAttributes DIRECTORY);
use Benchmark;
my ($x, @f);
$x = 0;
@f = qw(
C:\WINDOWS
C:\WINDOWS\WIN.INI
C:\WINDOWS\TEMP
C:\WINDOWS\not_a_file.txt
);
timethese( 10000, {
GetAtt => sub {
for (@f){
GetAttributes($_, $x);
$x = ($x > 0 and !($x & DIRECTORY)) ? 1 : 0;
}
},
fileTest => sub {
for (@f){
$x = -f $_ ? 1 : 0;
}
},
} );
Benchmark: timing 10000 iterations of GetAtt, fileTest...
GetAtt: 16 wallclock secs (16.04 usr + 0.00 sys = 16.04 CPU)
fileTest: 25 wallclock secs (24.39 usr + 0.00 sys = 24.39 CPU)
built-in -d and -f file test operators, which seems counter-intuitive
to me. Did I go astray somewhere in my testing (shown below for -f)?
Is there any reason not to switch to GetAttributes() when speed is an
issue? Perhaps this is well-known, but I didn't find much in a Google
search.
Chief S.
$^W = 1;
use strict;
use Win32::File qw(GetAttributes DIRECTORY);
use Benchmark;
my ($x, @f);
$x = 0;
@f = qw(
C:\WINDOWS
C:\WINDOWS\WIN.INI
C:\WINDOWS\TEMP
C:\WINDOWS\not_a_file.txt
);
timethese( 10000, {
GetAtt => sub {
for (@f){
GetAttributes($_, $x);
$x = ($x > 0 and !($x & DIRECTORY)) ? 1 : 0;
}
},
fileTest => sub {
for (@f){
$x = -f $_ ? 1 : 0;
}
},
} );
Benchmark: timing 10000 iterations of GetAtt, fileTest...
GetAtt: 16 wallclock secs (16.04 usr + 0.00 sys = 16.04 CPU)
fileTest: 25 wallclock secs (24.39 usr + 0.00 sys = 24.39 CPU)