A-Kay A-Kay - 9 months ago 74
Perl Question

Why is running opendir, readdir, stat so slow compared to the Windows dir command?

I have a Perl script that is using

to read the contents of a directory:

opendir ( DIR, $path ) or next;
while (my $file = readdir DIR) {

Then I'm doing:

  • -s $file
    to get the size of each file

  • (stat($file))[9]
    to get the modified time of each file

I'm running this from a Windows machine and accessing a Samba share on Ubuntu 14.04.

This is all working fine but the process seems to run very slow compared to when I run a
listing on the same folder.

Does anyone know why using
takes so much longer than a
listing and if there's any way I can change my script to speed it up?


According to perlport:

On Win32 stat() needs to open the file to determine the link count and update attributes that may have been changed through hard links. Setting ${^WIN32_SLOPPY_STAT} to a true value speeds up stat() by not performing this operation.

Since the files you're accessing are on a Samba share, opening them is probably fairly time consuming. Also, -s makes a stat system call behind the scenes, so calling -s followed by stat is wasteful.

The following should be faster:

local ${^WIN32_SLOPPY_STAT} = 1;

opendir my $dh, $path or die "Failed to opendir '$path': $!";

while (my $file = readdir $dh) {
    my ($size, $mtime) = (stat $file)[7, 9];

    say join "\t", $file, $size, $mtime;