Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does include() use file_exists()'s cache?

The question is quite simple: When it comes to touching the disk, are these two examples equal, or does scenario #2 touch the disk twice?

Scenario #1

include '/path/to/file.php';

Scenario #2

if (file_exists('/path/to/file.php'))
    include '/path/to/file.php';

I know that scenario #1 touches the disk once. Now, as I understand it file_exists() caches the path and whether or not the file exists. In order to clear that cache you need to call clearstatcache().

But does include, et alii, also use that cache? Or is it exclusive to file_exists()?

like image 281
Sverri M. Olsen Avatar asked Dec 23 '12 01:12

Sverri M. Olsen


2 Answers

Just one little thing to remind: include uses include path. file_exists doesn't. Apart from that you are obviously looking for problems instead of solutions (which must not be wrong, just saying, my answer might not fulfill what you look for, covers only a fragment).

like image 71
hakre Avatar answered Sep 20 '22 00:09

hakre


Both of these examples touch the disk twice – reading the directory and then reading the file. In the first example, this both happens during one command, the second command splits them. It’s very unlikely that the include() will read the directory again, as your OS should contain some sort of HD cache, that should last at least this long.

But you are obviously trying to over-optimize something. Unless you are going to this >100 times in your script, there will not be any performance-difference whatsoever between your two options.

like image 42
Chronial Avatar answered Sep 19 '22 00:09

Chronial