# require_once

- Thread: https://externals.io/message/28962
- Original archive: https://news-web.php.net/php.internals/28962
- Started by dletz@blog.de on 2007-04-24T09:07:40+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #28962 — dletz@blog.de — 2007-04-24T09:07:40+00:00

URL: https://externals.io/message/28962#28962

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

---

## #28963 — David Sklar — 2007-04-24T09:23:29+00:00

URL: https://externals.io/message/28962#28963 · In reply to: #28962

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


> 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

---

## #28964 — Antony Dovgal — 2007-04-24T10:10:03+00:00

URL: https://externals.io/message/28962#28964 · In reply to: #28962

> 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?

require_once() - 0.1315 sec
myrequire_once() - 0.7054 sec

I.e. your function is ~5 times slower. 

-- 
Wbr, 
Antony Dovgal

---

## #28965 — dletz@blog.de — 2007-04-24T10:17:51+00:00

URL: https://externals.io/message/28962#28965 · In reply to: #28964

Wow thats fast!

On my machine i get these results:
Win32 (PHP 5.2.1 CLI)

require_once - 1.7735 sec
myrequire_once - 1.0973 sec


is that with the sources i just send?

Am 24.04.2007, 12:10 Uhr, schrieb Antony Dovgal <antony@zend.com>:

> 
> > […]
>
> require_once() - 0.1315 sec
> myrequire_once() - 0.7054 sec
>
> I.e. your function is ~5 times slower.

---

## #28966 — Derick Rethans — 2007-04-24T10:52:38+00:00

URL: https://externals.io/message/28962#28966 · In reply to: #28965

> Wow thats fast!
> 
> On my machine i get these results:
> Win32 (PHP 5.2.1 CLI)

You use windows, you lose... Don't think that any sort of benchmarking 
on Windows makes sense...

regards,
Derick

---

## #28967 — Antony Dovgal — 2007-04-24T11:00:08+00:00

URL: https://externals.io/message/28962#28967 · In reply to: #28965

> Wow thats fast!
> 
> On my machine i get these results:
> Win32 (PHP 5.2.1 CLI)
> 
> require_once - 1.7735 sec
> myrequire_once - 1.0973 sec
> 
> 
> is that with the sources i just send?

Yes, of course.
But I'm using Linux, not windows.

-- 
Wbr, 
Antony Dovgal

---

## #28968 — Marco Tabini — 2007-04-24T11:01:46+00:00

URL: https://externals.io/message/28962#28968 · In reply to: #28967

>
> Yes, of course.
> But I'm using Linux, not windows.

Maybe you're even using a machine that is more powerful than his...


Mt.

---

## #28969 — Antony Dovgal — 2007-04-24T11:12:06+00:00

URL: https://externals.io/message/28962#28969 · In reply to: #28968

> 
> 
> > […]
> 
> Maybe you're even using a machine that is more powerful than his...

Maybe.
But I doubt that powerful machines execute internal functions faster than userspace ones.

-- 
Wbr, 
Antony Dovgal

---

## #28970 — Tijnema ! — 2007-04-24T11:56:10+00:00

URL: https://externals.io/message/28962#28970 · In reply to: #28967

> 
> > […]
>
> Yes, of course.
> But I'm using Linux, not windows.
>
> --
> Wbr,
> Antony Dovgal


That makes the real difference :)

On my 1.1Ghz AMD Linux server:
require_once: 0.2046
myrequire_once: 0.8541

And on my AMD 2.2 Ghz AMD Windows XP SP2:
require_once: 0.6525
myrequire_once: 0.9448

There's really no improvement on both platforms. But it's more
interesting that my 1.1Ghz PC running linux is triple as fast as my
2.2Ghz PC running Windows. (when using require_once)

Tijnema

---

## #29000 — mark@cyanox.nl — 2007-04-24T20:45:34+00:00

URL: https://externals.io/message/28962#29000 · In reply to: #28965

dletz@blog.de wrote:
> Wow thats fast!
>
> On my machine i get these results:
> Win32 (PHP 5.2.1 CLI)
>
> require_once - 1.7735 sec
> myrequire_once - 1.0973 sec 

I made two benchmarks one with relative paths and one with full paths.

The full paths:

<?
$start_time = microtime(true);
define('BASEPATH', dirname(__FILE__).'/');
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(BASEPATH.'manyClasses/'.$i.'.class.php');
    myrequire_once(BASEPATH.'manyClasses/'.$i.'.class.php');
}

$execution_time = microtime(true) - $start_time;
echo number_format($execution_time,4)."\n";
?>

Here the benchmarks with relative paths:


Ubuntu 6.10, intel core 2 duo 1.66Ghz (It only uses one core afaik).
PHP 5.2.0 (cli) (built: Mar 16 2007 19:40:08)
require_once        0.1123
myrequire_once   0.2582

Ubuntu 6.10, Pentium 4 3Ghz (Dual core also but probably only using one 
core).
PHP 5.1.6 (cli) (built: Mar  7 2007 12:48:25)
require_once        0.0984
myrequire_once   0.2800

Ubuntu 7.04, AMD 64bit 3700+.
PHP 5.2.1 (cli) (built: Feb 20 2007 20:11:58)
require_once        0.0663
myrequire_once   0.1639

Windows 2000, AMD 64bit 3200+.
PHP 5.1.4 (cli) (built: May  4 2006 10:35:22)
require_once        0.1590
myrequire_once   0.5190


With full paths:


Ubuntu 6.10, intel core 2 duo 1.66Ghz (It only uses one core afaik).
PHP 5.2.0 (cli) (built: Mar 16 2007 19:40:08)
require_once        0.1032
myrequire_once   0.3082

Ubuntu 6.10, Pentium 4 3Ghz (Dual core also but probably only using one 
core).
PHP 5.1.6 (cli) (built: Mar  7 2007 12:48:25)
require_once        0.0967
myrequire_once   0.2944

Ubuntu 7.04, AMD 64bit 3700+.
PHP 5.2.1 (cli) (built: Feb 20 2007 20:11:58)
require_once        0.0634
myrequire_once   0.1528

Windows 2000, AMD 64bit 3200+.
PHP 5.1.4 (cli) (built: May  4 2006 10:35:22)
require_once        0.2115
myrequire_once   0.6158

now I only have one windows system to test on but from this test it 
seems that the full path version is slower on windows then the relative 
paths. Which struck me as odd so I actually reran the tests several times.

The myrequire_once function is generally slower across my test cases 
then require_once.

---

## #29011 — Stanislav Malyshev — 2007-04-25T07:33:22+00:00

URL: https://externals.io/message/28962#29011 · In reply to: #28967

> > […]

Windows has very slow filesystem layer (compared to Linux), 
unfortunately. Which means things that do more filesystem (like 
require_once that should resolve file pathes) are slower. Realpath cache 
should make it better to a measure.
Anyway, the code you have sent is incorrect since it does not resolve 
and does not canonicalize pathnames. For example, on Windows it would 
consider names written in different case different file names (of 
course, this is only one problem of many).
-- 
Stanislav Malyshev, Zend Products Engineer
stas@zend.com  http://www.zend.com/
