file_exists vs is_file (non existing files)

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

f1 is slower than f2 by 53.52%

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/apasswd');
  }
}
function f2() {
  for($i=0; $i<LOOP; ++$i) {
    @is_file('/etc/apasswd');
  }
}
$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
8.03%564293452233875.3.3-7+squeeze1
56.58%1004897641768php-5.3.6-phpbrew_compiled
53.87%1067558693802php-5.3.7-phpbrew_compiled
50.64%1075701714094php-5.3.8-phpbrew_compiled
46.8%1120970763583php-5.3.9-phpbrew_compiled
53.94%1051941683333php-5.3.10-phpbrew_compiled
49.86%1112582742404php-5.3.11-phpbrew_compiled
49.54%1113287744450php-5.3.12-phpbrew_compiled
37.74%1033390750248php-5.3.13-phpbrew_compiled
55.84%1091022700099php-5.3.14-phpbrew_compiled
49.78%1068538713386php-5.3.15-phpbrew_compiled
62.74%1118794687461php-5.3.16-phpbrew_compiled
64.97%1110892673398php-5.3.17-phpbrew_compiled
54.89%1123041725055php-5.3.18-phpbrew_compiled
55.93%1025693657797php-5.3.19-phpbrew_compiled
63.32%825660505537php-5.4.0-phpbrew_compiled
49.19%833135558424php-5.4.1-phpbrew_compiled
64.52%811845493466php-5.4.2-phpbrew_compiled
49.99%866225577524php-5.4.3-phpbrew_compiled
72.83%833966482537php-5.4.4-phpbrew_compiled
41.25%851261602653php-5.4.5-phpbrew_compiled
69.74%824486485737php-5.4.6-phpbrew_compiled
59.01%808946508738php-5.4.7-phpbrew_compiled
56.66%798582509762php-5.4.8-phpbrew_compiled
60.29%844227526687php-5.4.9-phpbrew_compiled
50%
updown
50%

Comments

Submitted by Hani on Sat, 03/24/2012 - 20:17

shut-up operator is slowing execution down but it's slowing it down equally for both functions (tested). The ratio of the time difference between f1() and f2() will be the same when @ operator is removed.