Search code examples
phpfile-exists

CPU load with file_exists in php


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?


Solution

  • 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