Re: require_once

From: Date: Tue, 24 Apr 2007 09:23:29 +0000
Subject: Re: require_once
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-28963@lists.php.net to get a copy of this message
require_once verifies (via stat()) each path component in the path being loaded so that it can canonicalize it to check it against the canonicalized list of already-required files. Run your script below under strace and you will see all of those stats gumming up the works. Note also that if you want maximum performance here, pass full paths, not relative paths, to include/require to avoid any include_path overhead. David On 4/24/07, dletz@blog.de <dletz@blog.de> wrote:
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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.internals (#28963) next »