file_exists() 在 PHP 中太慢了.任何人都可以提出更快的替代方案吗? [英] file_exists() is too slow in PHP. Can anyone suggest a faster alternative?
问题描述
在我们的网站上显示图像时,我们会通过调用 file_exists()
来检查文件是否存在.如果文件丢失,我们将退回到虚拟图像.
When displaying images on our website, we check if the file exists with a call to file_exists()
. We fall back to a dummy image if the file was missing.
然而,分析表明这是生成页面中最慢的部分,file_exists()
每个文件占用 1/2 毫秒.我们只测试了 40 个左右的文件,但这仍然会将 20 毫秒拖入页面加载时间.
However, profiling has shown that this is the slowest part of generating our pages with file_exists()
taking up to 1/2 ms per file. We are only testing 40 or so files, but this still pushes 20ms onto the page load time.
任何人都可以提出一种加快速度的方法吗?是否有更好的方法来测试文件是否存在?如果我建立某种缓存,我应该如何保持同步.
Can anyone suggest a way of making this go faster? Is there a better way of testing if the file is present? If I build a cache of some kind, how should I keep it in sync.
推荐答案
file_exists()
应该是一个非常便宜的操作.另请注意,file_exists
会构建自己的缓存以帮助提高性能.
file_exists()
should be a very inexpensive operation. Note too that file_exists
builds its own cache to help with performance.
参见:http://php.net/manual/en/function.file-exists.php
这篇关于file_exists() 在 PHP 中太慢了.任何人都可以提出更快的替代方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!