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
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
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
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:
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 secI.e. your function is ~5 times slower.
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
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 secis that with the sources i just send?
Yes, of course.
But I'm using Linux, not windows.
--
Wbr,
Antony Dovgal
Yes, of course.
But I'm using Linux, not windows.
Maybe you're even using a machine that is more powerful than his...
Mt.
Yes, of course.
But I'm using Linux, not windows.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
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 secis that with the sources i just send?
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
On my machine i get these results:
Win32 (PHP 5.2.1 CLI)
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/
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.