There is no way to find out this information without stat()
ing every file in the directory. It is slow if you have this many files in a single directory. stat()
needs to retrieve the inode of every single file.
Adding more memory might help due to caching.