Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file_exists() is up to 100x slower then is_dir() or is_file() #36493

Closed
staabm opened this issue Apr 19, 2020 · 1 comment · Fixed by #36556
Closed

file_exists() is up to 100x slower then is_dir() or is_file() #36493

staabm opened this issue Apr 19, 2020 · 1 comment · Fixed by #36556

Comments

@staabm
Copy link
Contributor

staabm commented Apr 19, 2020

Inspired by https://bugs.php.net/bug.php?id=78285

benchmark:

<?php
$t0 = hrtime(true);
for ($i = 0; $i < 1000; $i++) {
    is_file(__FILE__);
}
$t1 = hrtime(true);
for ($i = 0; $i < 1000; $i++) {
    file_exists(__FILE__);
}
$t2 = hrtime(true);
printf("is_file      took %f ms\n", ($t1 - $t0)/10e6);
printf("file_exists took %f ms\n", ($t2 - $t1)/10e6);

measured on my laptop

$ php perf.php
is_file      took 0.058450 ms
file_exists took 5.852210 ms

$ php perf.php
is_file      took 0.047010 ms
file_exists took 6.005810 ms

$ php -v
PHP 7.3.0 (cli) (built: Dec  6 2018 02:17:00) ( ZTS MSVC15 (Visual C++ 2017) x86
 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.0-dev, Copyright (c) 1998-2018 Zend Technologies
    with blackfire v1.24.1~win-x32-zts73, https://blackfire.io, by Blackfire

Did a quick search thru the codebase and found looots of file_exists() calls.

Maybe someone is interessted to investigate further

@HeahDude
Copy link
Contributor

HeahDude commented Apr 19, 2020

file_exists also resolves links and dirs. Even if there is a gain in using is_file one should be careful to not break anything.

@fabpot fabpot closed this as completed Apr 24, 2020
fabpot added a commit that referenced this issue Apr 24, 2020
…icolas-grekas)

This PR was merged into the 5.1-dev branch.

Discussion
----------

Use is_file() instead of file_exists() where possible

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36493
| License       | MIT
| Doc PR        | -

Thanks to caching, `is_file()` is much faster than `file_exists()`.
I screened the codebase for places where it could matter, here they are.

Commits
-------

f38904e Use is_file() instead of file_exists() where possible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants