How to read directory in PHP fast

We need to get a list of specific files from a directory.

Say we need to find all *.php files in our c:\inetpub\wwwroot folder. There are a couple of options to do it in PHP: opendir(), glob(), scandir() and RecursiveDirectoryIterator class. If we need to search through subfolders then the simplest solution in my opinion is to use the RecursiveDirectoryIterator class.

But there is a faster solution based on scandir() php buildin function. This recursive function is about 5% faster in my tests then the previous solution.

My results searching for 2173 php-files through c:\inetpub\wwwroot directory with totally ~90000 files in 10927 folders.

readdirRecursiveDirectory - 42sec
readdirScandir - 39sec