is_file/file_exists 性能和缓存 [英] is_file/file_exists performance and cache

查看:41
本文介绍了is_file/file_exists 性能和缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些测试来比较和测量两个函数的速度.is_file 似乎比 file_exists 快几倍(我对两者都使用了 10000 次迭代).我想知道 PHP 或 OS 是否为这些功能使用了一些缓存,还是总是访问 HDD?我想不会,但我想知道...

I made some tests to compare and measure speed of both functions. is_file seems to be several times faster (I used 10000 iterations for both) than file_exists . I wonder if PHP or OS use some cache for these functions or does is always access HDD ? I think no, but I wonder...

我使用了这个代码:

<?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>';
?>

推荐答案

当您使用 stat()、lstat() 或任何中列出的其他功能受影响的函数列表(如下),PHP缓存这些函数的信息返回以便更快地提供表现.然而,在某些在这种情况下,您可能想要清除缓存的信息.例如,如果正在检查相同的文件在单个脚本中多次,并且该文件有被删除的危险在此期间删除或更改脚本的操作,您可以选择清除状态缓存.在这些情况下,您可以使用clearstatcache() 函数来清除PHP缓存的关于一个的信息文件.

When you use stat(), lstat(), or any of the other functions listed in the affected functions list (below), PHP caches the information those functions return in order to provide faster performance. However, in certain cases, you may want to clear the cached information. For instance, if the same file is being checked multiple times within a single script, and that file is in danger of being removed or changed during that script's operation, you may elect to clear the status cache. In these cases, you can use the clearstatcache() function to clear the information that PHP caches about a file.

受影响的函数包括 stat()、lstat()、file_exists()、is_writable()、is_readable(), is_executable(),is_file(), is_dir(), is_link(),文件时间(),文件时间(),文件时间(),文件节点()、文件组()、文件所有者()、文件大小()、文件类型()和文件权限().

Affected functions include stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), and fileperms().

这篇关于is_file/file_exists 性能和缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆