Jump to content

Recommended Posts

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]

Link to comment
https://forums.phpfreaks.com/topic/161838-require-vs-require_once/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.