Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is faster: glob() or opendir()

Which is faster between glob() and opendir(), for reading around 1-2K file(s)?

like image 717
ariefbayu Avatar asked May 04 '10 06:05

ariefbayu


People also ask

Why is glob function used?

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.

How do I open a directory in PHP?

The opendir() function opens a directory handle.


2 Answers

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.

like image 78
Ben Rowe Avatar answered Oct 20 '22 11:10

Ben Rowe


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:

  • http://www.phparch.com/2010/04/28/putting-glob-to-the-test/
like image 25
Gordon Avatar answered Oct 20 '22 11:10

Gordon