require_once

From: Date: Tue, 24 Apr 2007 09:07:40 +0000
Subject: require_once
Groups: php.internals 
Request: Send a blank email to internals+get-28962@lists.php.net to get a copy of this message
Hello everyone, when testing speed of includes and requires i found that "require_once" seems to be very slow in comparison to "require". Even worse "require_once" seems to be slower than a php function "myrequire_once". This is true not only for cli but also when using apc or eAccelerator. Does anyone has an explanation for this? Testcode: <? $start_time = microtime(true); function myrequire_once($file) {
    static $loaded = array();
    if (!array_key_exists($file, $loaded))
    {
        $loaded[$file] = 1;
        require $file;
    }
} for ($i=0;$i<1000;$i++) {
    require_once('manyClasses/'.$i.'.class.php');
    //myrequire_once('manyClasses/'.$i.'.class.php');
} $execution_time = microtime(true) - $start_time; echo number_format($execution_time,4)."\n"; ?> I first generated the classes in manyClasses with this generation code (you have to create the directory manyClasses first): <? $template = '<? class A%s {
    private $B%s = "%s";
    function C%s($arg1, $arg2)
    {
        if (rand() > 1000)
            return $arg1 + $arg2;
        else
            return 0;
    }
}; ?>'; for($i=0; $i< 1000; $i++) {
    $rand = rand();
    $file = sprintf($template, md5($rand), md5($rand+1), md5($rand+2), md5($rand+3));
    file_put_contents('manyClasses/'.$i.'.class.php', $file);
} ?> Faithfully Dominic Letz

« previous php.internals (#28962) next »