php 中 file_exists 的 CPU 负载
我拥有一个每分钟 cpu httpd 请求负载很高的网站。我注意到我在每个 httpd 请求上使用“file_exists”。这个功能是不是太重了?
i own a site with a high load cpu httpd request per minute. I've noticed i use "file_exists" on each httpd request. Is that function to much heavy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此函数只会检查文件是否存在 - 这意味着对磁盘的访问(可能需要一点时间,但也不会花太多时间)
考虑到您的应用程序可能由数十个组成(如果不是数百个) 个 PHP 文件,每个请求都必须读取这些文件,我认为一个
file_exists
没有任何区别。(嗯,至少,只要您检查本地磁盘上的文件 - 不通过任何网络驱动器或类似的东西)
旁注:如果您想确定 PHP 脚本中的 CPU 消耗情况,您可能会对 Xdebug 扩展,它提供了分析功能。
您可以阅读我不久前给出的这个答案,它很长: 我如何测量用 php 编写的代码的速度? -- 我不会在这里复制粘贴它。
您可能还想阅读我对该问题的回答(我在其中写了有关 Xdebug 和分析的部分):优化基于 Kohana 的网站以提高速度和可扩展性
This function will only check of a file exists -- which means an access to the disk (which might take a little time, but not that much either)
Considering your application is probably made of dozens (if not hundreds) of PHP files, which all have to be read for each request, I don't think one
file_exists
makes any difference.(Well, at least, as long as your are checking for a file on a local disk -- not going through any network drive or anything like that)
As a sidenote : if you want to identify where CPU is spend in your PHP scripts, you might be interested by the Xdebug extension, which provides a profiling functionnality.
You can read this answer I gave some time ago, which is quite long : How can I measure the speed of code written in php? -- I won't copy-paste it here.
You might also want to read my answer to that question (there is a section where I wrote about Xdebug and profiling) : Optimizing Kohana-based Websites for Speed and Scalability
现实地说,玩“猜测瓶颈”可能是一项相当徒劳的任务 - 我建议使用分析器,例如 Zend Studio。
Being realistic, playing 'guess the bottleneck' is likely to be a pretty fruitless task - I'd recommend using a profiler, such as the one built into Zend Studio.
file_exists 通常非常便宜,特别是因为结果缓存在 php 的统计缓存.. 像重型数据库这样的领域往往是 cpu 的最大消耗者。
尝试进行一些分析来确定应用程序的哪一部分使用时间最多,这里有一些示例:
http://www.ibm.com/developerworks/opensource/library/os-php-fastapps2/
file_exists is typically very cheap, especially since the result is cached in php's stat cache.. areas like heavy DB tend to be the largest consumer of cpu.
try some profiling to determine what part of your app is using up the most time, some examples here:
http://www.ibm.com/developerworks/opensource/library/os-php-fastapps2/