我做了一些基准测试,但在获得结果之前,让我们看看这些函数是如何工作的。你可以阅读PHP源代码here。有一个这个答案的法语版本,本周早些时候写的,时机很好;)。
我将会说is_file()
也是如此,因为它被定义到源代码中的相同核心函数中。对于核心功能,我说的是 C 源代码,无法从 PHP 语言访问到您的脚本中。
据我了解,file_exists()
and is_file()
是核心函数的子函数php_stat()
。这是该过程的高度简化的伪代码:
function php_stat($file)
{
'file_exists'
↳ virtual_file_ex($file)
↳ virtual_access($file)
'Windows'
↳ tsrm_win32_access($file)
↳ return access($file)
'Other systems'
↳ return access($file)
'is_file'
↳ return $file.st_mode == S_IFREG
}
以及伪代码stream_resolve_include_path()
过程:
function stream_resolve_include_path($file)
{
zend_resolve_path($file)
↳ php_resolve_path_for_zend($file)
↳ php_resolve_path($file)
↳ tsrm_realpath($file)
↳ return estrdup($file)
}
从这里,即使没有基准的数字结果,您也可以看到一个函数的资源消耗有多大。
基准测试代码:
function bench_file($file) {
$res = array();
$max = 1000000;
// is_file()
$res[] = microtime(1);
for ( $i = 0; $i < $max; ++$i ) {
if ( is_file($file) ) {
//
}
}
$res[] = microtime(1);
clearstatcache();
// file_exists()
$res[] = microtime(1);
for ( $i = 0; $i < $max; ++$i ) {
if ( file_exists($file) ) {
//
}
}
$res[] = microtime(1);
clearstatcache();
// stream_resolve_include_path()
$res[] = microtime(1);
for ( $i = 0; $i < $max; ++$i ) {
if ( stream_resolve_include_path($file) !== false ) {
//
}
}
$res[] = microtime(1);
printf(
'is_file = %f, file_exists = %f, stream_resolve_include_path = %f',
$res[1] - $res[0], $res[3] - $res[2], $res[5] - $res[4]
);
}
让我们用一个存在的文件 (1) 和一个不存在的文件 (2) 进行测试:
1 : is_file = 0.218582, file_exists = 0.742195, stream_resolve_include_path = 1.626521
2 : is_file = 0.458983, file_exists = 0.644638, stream_resolve_include_path = 5.623289
结果不言而喻;)
Benchmark v2 - 只是添加新功能进行测试的更简单方法。
function micro($func, $file) {
$max = 1000000;
$start = microtime(1);
for ( $i = 0; $i < $max; ++$i ) {
if ( $func($file) ) {
//
}
}
$end = microtime(1);
clearstatcache();
return $end - $start;
}
function bench_file($file) {
$res = array(
'is_file' => micro('is_file', $file),
'file_exists' => micro('file_exists', $file),
'stream_resolve_include_path' => micro('stream_resolve_include_path', $file)
);
$ret = '';
foreach ( $res as $key => $value ) {
$ret .= sprintf('%s = %f, ', $key, $value);
}
return trim($ret, ', ');
}
echo '<pre>', bench_file('file-ok'), "\n", bench_file('file-ko'), '</pre>';
Results:
is_file = 0.295752, file_exists = 0.852082, stream_resolve_include_path = 1.759607
is_file = 0.527770, file_exists = 0.724793, stream_resolve_include_path = 5.916151
打电话有一点费用$funct()
,这解释了数字略高的原因。