我做了一些测试来比较和测量两种功能的速度.is_file似乎比file_exists快几倍(我使用10000次迭代).我想知道PHP或OS是否为这些功能使用了一些缓存,或者是否始终访问HDD?我想不,但我想知道......
我用过这段代码:
<?php
$time = microtime();
$time = explode(' ', $time);
$begintime = $time[1] + $time[0];
for($i=0;$i<10000;$i++)
file_exists('/Applications/MAMP/htdocs/index.php');
$time = microtime();
$time = explode(" ", $time);
$endtime = $time[1] + $time[0];
$totaltime = ($endtime - $begintime);
echo 'PHP parsed this in ' .$totaltime. ' seconds.</br>';
$time = microtime();
$time = explode(" ", $time);
$begintime = $time[1] + $time[0];
for($i=0;$i<10000;$i++)
is_file('/Applications/MAMP/htdocs/index.php');
$time = microtime();
$time = explode(" ", $time);
$endtime = $time[1] + $time[0];
$totaltime = ($endtime - $begintime);
echo 'PHP parsed this in ' .$totaltime. ' seconds.</br>';
?>
Run Code Online (Sandbox Code Playgroud)
当您使用stat(),lstat()或受影响的函数列表(下面)中列出的任何其他函数时,PHP会缓存这些函数返回的信息,以便提供更快的性能.但是,在某些情况下,您可能希望清除缓存的信息.例如,如果在单个脚本中多次检查同一文件,并且该文件在该脚本操作期间有被删除或更改的危险,则可以选择清除状态缓存.在这些情况下,您可以使用clearstatcache()函数清除PHP缓存文件的信息.
受影响的函数包括stat(),lstat(),file_exists(),is_writable(),is_readable(),is_executable(),is_file(),is_dir(),is_link(),filectime(),fileatime(),filemtime() ,fileinode(),filegroup(),fileowner(),filesize(),filetype()和fileperms().
| 归档时间: |
|
| 查看次数: |
13689 次 |
| 最近记录: |