phpfrizzeak Posted June 11, 2009 Share Posted June 11, 2009 My benchmarks from require vs. require_once show require_once to be margianally faster even though it performs an lstat()-like call (puzzling). What could I be doing wrong? I am pretty sure APC is DISABLED, but even if it were not what gives?: #!/usr/bin/php <?php $fileNamesRequireOnce = array(); $fileNamesRequire = array(); for($i=0; $i<1000; $i++) { $fileName = dirname(__FILE__) . '/_test_file_' . $i . ".php"; if ($i%2 == 0) { $fileNamesRequireOnce[] = $fileName; } else { $fileNamesRequire[] = $fileName; } $fileHandler = fopen($fileName, 'w'); fwrite($fileHandler, "<?php class TestClass_" . $i . " { /* ... */ } ?>\n"); fclose($fileHandler); } $fileNames = array_merge($fileNamesRequire, $fileNamesRequireOnce); $timeStart = microtime(true); foreach ($fileNamesRequireOnce as $fileName) { require_once($fileName); } $timeStop = microtime(true); $rUsage = getrusage(); $userTime = $rUsage["ru_utime.tv_usec"]/1e6; echo "require_once x" . (count($fileNamesRequireOnce)) . " [" . ($timeStop - $timeStart) . "] with user time [" . $userTime . "]\n"; $timeStart = microtime(true); foreach ($fileNamesRequire as $fileName) { require($fileName); } $timeStop = microtime(true); $rUsage = getrusage(); $userTime = $rUsage["ru_utime.tv_usec"]/1e6; echo "require x" . (count($fileNamesRequire)) . " [" . ($timeStop - $timeStart) . "] with user time [" . $userTime . "]\n"; foreach($fileNames as $fileName) { unlink($fileName); } Results are as follows: require_once x500 [0.3516731262207] with user time [0.067989] require x500 [0.3381929397583] with user time [0.092985] Quote Link to comment https://forums.phpfreaks.com/topic/161838-require-vs-require_once/ Share on other sites More sharing options...
phpfrizzeak Posted June 11, 2009 Author Share Posted June 11, 2009 Ooops... looked at the numbers and see a SLIGHT difference in favor of require... I still do not understand where there are no sharp distinctions (even if I dramatically increase the number of included files). Quote Link to comment https://forums.phpfreaks.com/topic/161838-require-vs-require_once/#findComment-853867 Share on other sites More sharing options...
Maq Posted June 11, 2009 Share Posted June 11, 2009 From the manual: The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again. There you go, require_once() does an extra check to see if the file has already been included. Quote Link to comment https://forums.phpfreaks.com/topic/161838-require-vs-require_once/#findComment-853872 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.