问题描述
我做了一些测试来比较和测量两个函数的速度.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()、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 性能和缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!