开发者

how to read a cached xml file?

开发者 https://www.devze.com 2022-12-21 07:25 出处:网络
have a file caching system for a php 开发者_运维技巧5 library i use often. when the request is made i check for a cached file, if there is one i render it and exit.

have a file caching system for a php 开发者_运维技巧5 library i use often. when the request is made i check for a cached file, if there is one i render it and exit.

$contents = file_get_contents( self::_cacheFile() );
echo $contents;
exit();     

i have to do file_get_contents instead of just include because of cached xml files with the pesky

`<?xml version="1.0"?>` 

is there a better way to pull in my cache files without shorttags firing?


Since include will evaluate the content of the files, e.g. run in through the PHP interpreter and also use the include_path to find files, I'd say include is slower. file_get_contents will just treat the contents of a file as string. Less overhead, more speed.

From the manual page:

file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.

However, if you are after outputting the file, instead of getting it into a string, readfile() is even a tiny bit faster than file_get_contents. Given that include'ing will output any non PHP content as well, this likely more likely what you are after I guess.

Revised benchmark on my desktop machine:

$start1 = microtime(1);
for($i=0; $i<100000; $i++) {
    include 'log.txt';
}
$end1 = microtime(1) - $start1;

and

$start2 = microtime(1);
for($i=0; $i<100000; $i++) {
    echo file_get_contents('log.txt');
}
$end2 = microtime(1) - $start2;

and

$start3 = microtime(1);
for($i=0; $i<100000; $i++) {
    readfile('log.txt');
}
$end3 = microtime(1) - $start3;

Result

echo PHP_EOL, $end1, // 137.577358961
     PHP_EOL, $end2, // 136.229552984
     PHP_EOL, $end3; // 136.849179029


Nothing beats a (well made) benchmark (which is harder than it looks, I might be overlooking something). Still as both are made in the same conditions they should serve as a measuring stick.

test.txt is a 12kB, 876 lines text file:

vinko@parrot:~$ ls -la test.txt ; wc -l test.txt
-rw-r--r-- 1 vinko vinko 12264 2010-02-24 19:08 test.txt
876 test.txt

file_get_contents.php:

vinko@parrot:~$ more file_get_contents.php
<?php
echo file_get_contents("test.txt");
?>

include.php

vinko@parrot:~$ more include.php
<?php
include("test.txt");
?>

readfile.php

vinko@parrot:~$ more readfile.php
<?php
readfile("test.txt");
?>

So, we time the execution of 10 thousand iterations of each:

vinko@parrot:~$ time for i in `seq 10000`; do php file_get_contents.php >/dev/null; done

real    3m57.895s
user    2m35.380s
sys     1m15.080s

vinko@parrot:~$ time for i in `seq 10000`; do php include.php >/dev/null; done

real    3m57.919s
user    2m37.040s
sys     1m16.780s

vinko@parrot:~$ time for i in `seq 10000`; do php readfile.php >/dev/null; done 
real    3m57.620s
user    2m38.400s
sys     1m14.100s

Conclusion: All three are practically equivalent for 12 kB text files on PHP 5.2.4 with Suhosin Patch.


file_get_contents and include doesn't do the same thing:

  • file_get_contents reads the content of a file, and returns it as a string
  • include will execute the content of the file.

About the speed, without an opcode cache, I suppose file_get_contents should theoretically be faster, as it does less calculation (no compilation/execution of the code).

Still, what matters the most is probably what you are trying to do: if you only want to read a file, you should use file_get_contents.


If all you want to do is output the file contents, you should be using readfile(). This is faster and less memory intensive than file_get_contents()


thanks for the tip, for those who are curious

readfile();
<!-- dynamic page rendered in 0.133193016052 seconds.-->
<!-- static page rendered in 0.00292587280273 seconds.-->

vs.

file_get_contents();
<!-- dynamic page rendered in 0.133193016052 seconds.-->
<!-- static page rendered in 0.00303602218628 seconds.-->

vs.

include();
<!-- dynamic page rendered in 0.133193016052 seconds.-->
<!-- static page rendered in 0.00348496437073 seconds.-->


file_get_contents will be the fastest way to retrieve the cached file for a couple reasons:

  1. It performs no processing on the data it loads (parsing PHP code, handling newlines etc.)
  2. It uses optimized techniques to load the files as fast as possible (memory mapped files for one).


include faster and incredible fast if you using opcache (apc), than opcache keep in super cache (your ram ddr3200 etc. ) and loading is instant 0.03ms for 1mln request > General this is true option to using any cache, any features optymalization for php. Include proccessing script data

readfile reading everythink included headers of file like for files: pdf, txt. MIME are included, good for readfile together with async sending ( example, if you downlaoding videos, don't need wait on full load to php, it flush to webbrowser immiedtly all data from file, php become like tunel between browser, and disk. Difference between NGINX and PHP for liek that file is in PHP you can controlle session, auth, stats download, etc. before someone get access to file. (NGINX is better if you don't need checking access to file, NGIXN can using own cache, and cache for webbrowser for static files)

file_get_contents -> is just like wget, depend from disk speed, you can download from URL itp. or path of disk, you can .php take file is saftly far from processing by php. Example If you want .jpg but if you do by include and in .jpg will be <?php this will be checking by php script. So file_get_contents is just saftly

== SUMMARY ==

all functions are almost the same fast as your disk speed

  1. if your file is big and is media or has MIME included use readfile ( better NGINX use to static files but if you want hide ori path,etc. php is perfect)
  2. if you want just download data from file or url use file_get_contents
  3. if you using script, txt, or css, html etc. to using in php using include to php server etc. could cache and boost files with opcache or apc.

but if you don't using any cache, and just want download file all function is the same almost, difference is just in C++ in extra options which can do if exitsts

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号