Win32::File::GetAttributes() faster than -f and -d

  • Thread starter Chief Squawtendrawpet
  • Start date
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top