Which is faster between glob()
and opendir()
, for reading around 1-2K file(s)?
glob (short for global) is used to return all file paths that match a specific pattern. We can use glob to search for a specific file pattern, or perhaps more usefully, search for files where the filename matches a certain pattern by using wildcard characters.
The opendir() function opens a directory handle.
http://code2design.com/forums/glob_vs_opendir
Obviously opendir()
should be (and is) quicker as it opens the directory handler and lets you iterate. Because glob()
has to parse the first argument it's going to take some more time (plus glob
handles recursive directories so it'll scan subdirs, which will add to the execution time.
glob
and opendir
do different things. glob
finds pathnames matching a pattern and returns these in an array, while opendir
returns a directory handle only. To get the same results as with glob
you have to call additional functions, which you have to take into account when benchmarking, especially if this includes pattern matching.
Bill Karwin has written an article about this recently. See:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With