file_exists vs is_file (existing files)

Submitted by Lito on Fri, 03/16/2012 - 15:38

f1 is slower than f2 by 371.63% (3.7 times slower)

Full source code as executed.
/* How many times the code under test should run in each function */
define('LOOP',1000000);
 
function f1() {
  for($i=0; $i<LOOP; ++$i) {
    file_exists('/etc/passwd');
  }
}
function f2() {
  for($i=0; $i<LOOP; ++$i) {
    is_file('/etc/passwd');
  }
}
$start = microtime(true);
f1();
$stop = microtime(true);
$time1 = $stop - $start;
 
$start = microtime(true);
f2();
$stop = microtime(true);
$time2 = $stop - $start;
 
echo $time1 . "\t";
echo $time2 . "\n";
Test results
comparisontime1time2php_version
150.73%401767916023975.3.3-7+squeeze1
332.6%833410192651php-5.3.6-phpbrew_compiled
346%874252196020php-5.3.7-phpbrew_compiled
333.31%841630194231php-5.3.8-phpbrew_compiled
330.49%857502199190php-5.3.9-phpbrew_compiled
344.41%870705195923php-5.3.10-phpbrew_compiled
345.32%873676196189php-5.3.11-phpbrew_compiled
346.89%874665195721php-5.3.12-phpbrew_compiled
316.87%808626193977php-5.3.13-phpbrew_compiled
364.14%897726193419php-5.3.14-phpbrew_compiled
327.74%847269198080php-5.3.15-phpbrew_compiled
370.53%934795198669php-5.3.16-phpbrew_compiled
369.6%918769195650php-5.3.17-phpbrew_compiled
316.71%941020225823php-5.3.18-phpbrew_compiled
341.23%861238195189php-5.3.19-phpbrew_compiled
446.03%801890146857php-5.4.0-phpbrew_compiled
411.77%803680157040php-5.4.1-phpbrew_compiled
451.64%807662146412php-5.4.2-phpbrew_compiled
405.73%824074162948php-5.4.3-phpbrew_compiled
467.73%801897141245php-5.4.4-phpbrew_compiled
406.83%813766160560php-5.4.5-phpbrew_compiled
452.06%801300145146php-5.4.6-phpbrew_compiled
454.38%796180143617php-5.4.7-phpbrew_compiled
458.48%798118142908php-5.4.8-phpbrew_compiled
399.53%800630160278php-5.4.9-phpbrew_compiled
50%
updown
50%