Re: OPTIMIZING - The fastest way to open and show a file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Lynch wrote:

On Fri, October 14, 2005 6:29 am, Ruben Rubio Rey wrote:
  if(file_exists($filename)){
        $modified_date=filemtime($filename);
        if(time()<($modified_date+1 * 24 * 60 * 60)){
            $handle = fopen($filename, "r");
            $contents = fread($handle, filesize($filename));
            fclose($handle);
            echo $contents;
        }
     }

Checking both file_exists and then doing fopen seems a bit silly.

Trap the error from fopen, and just use that as your file_exists test.

I suspect http://php.net/file_get_contents will be SLIGHTLY faster
than doing all of this code, though:

if (filemtime($filename) > time()) $contents =
@file_get_contents($filename);
if ($contents === false){
 //error-handling code
}
else{
 echo $contents;
}

Then, of course, we have to wonder if you NEED $contents for later use
in the script.

If not, something like this will clock in better:

$bytes = @readfile($filename);
if ($bytes === false){
 //error-handling code
}

This seems to be the best solution. I do not need $content anymore. I ll post results. :)

The difference here is that you don't even stuff the file into the PHP
string.  It's all read and passed out to stdout in low-level internal
PHP C code, and the data never needs to hit "PHP" variables which are
"more expensive" to setup and maintain.

Note that which is REALLY fastest will probably depend on the size of
the files, your OS system cache, your hardware, and maybe which
version of PHP you are using, if the underlying functions changed.

Must be nice to be worried about 0.0x milliseconds -- I'm fighting a
mystery 3.0 seconds in a data feed for a search engine myself :-)
Search engine ... thats the most complicated!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux