in reply to Quickly checking if a directory is empty

Rather than using glob which probably is going ahead and reading the entire directory under the covers, you might try just calling opendir and then check if readdir returns more than 2 items (last'ing out on the 3rd; presuming of course a POSIX-y at least "." and ".." even in an empty dir). Alternately stat the directory itself and see if the nlink field is greater than 2.

And as a somewhat related aside: don't put thousands of files in a single directory if you can avoid it as for most filesystems it'll cause performance problems (as you've found). Break up files into groups (e.g. based on part of the filename, or artificially generate a key (Digest::MD5 is useful) and break that key up (directories 00, 01, ..., fe, ff, files with md5s starting with ab go in the ab directory, yadda yadda yadda)).

The cake is a lie.
The cake is a lie.
The cake is a lie.

  • Comment on Re: Quickly checking if a directory is empty