require_once против require

июня 26, 2007 by admin

Заинтересовала разница в скорости между require(include)/require_once(include_once). Разницы не заметил. Может что-то с тестом?

2.94671392441 [include]
2.9369969368 [include_once]
3.1668279171 [require]
2.69922208786 [require_once]

Система: Apache/2.2.3 (Ubuntu) PHP/5.2.1 Server

Собственно сам тест:

<?php
error_reporting(E_ALL);
define(’REP_CNT’,300);

function get_rnd_php_str()
{
    return ’$i=’.rand().’; $i++; $i=”‘.md5(rand()).’”;’;
}

function prepare_file($base_dir, $str_cnt = 100)
{
    $filename = $base_dir . ’/' .md5(rand());
    $handle = fopen($filename,’w+’);
    fwrite($handle, ”<?php\n”);
    for($i = 0; $i < $str_cnt; $i++) {
        fwrite($handle, get_rnd_php_str());
    }
    fclose($handle);
    return $filename;
}

function refresh_file($old_name)
{
    $new_name = dirname($old_name) . ’/' .md5(rand());
    rename($old_name,$new_name);
    $handle = fopen($new_name,’a+’);
    fwrite($handle, get_rnd_php_str());
    fclose($handle);
    return $new_name;
}

$t1 = microtime(1);
$file = prepare_file(’/tmp’);
$cnt = REP_CNT;
while($cnt–) {
    require_once($file);
    $file = refresh_file($file);
}
unlink($file);

$t2 = microtime(1);
$file = prepare_file(’/tmp’);
$cnt = REP_CNT;
while($cnt–) {
    require($file);
    $file = refresh_file($file);
}
unlink($file);

$t3 = microtime(1);
$file = prepare_file(’/tmp’);
$cnt = REP_CNT;
while($cnt–) {
    include_once($file);
    $file = refresh_file($file);
}
unlink($file);

$t4 = microtime(1);
$file = prepare_file(’/tmp’);
$cnt = REP_CNT;
while($cnt–) {
    include($file);
    $file = refresh_file($file);
}
unlink($file);
$t5 = microtime(1);

echo ($t5 - $t4) . ’ [include]<br />’;
echo ($t4 - $t3) . ’ [include_once]<br />’;
echo ($t3 - $t2) . ’ [require]<br />’;
echo ($t2 - $t1) . ’ [require_once]<br />’;
echo ’<br />’;

highlight_file(__FILE__);

Статьи по теме: require_once против require

Posted in Без рубрики |

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.