Показать сообщение отдельно

  #9  
Старый 21.03.2009, 17:51
imajo.ati
Участник форума
Регистрация: 21.02.2008
Сообщений: 255
С нами: 9589849

Репутация: 177
По умолчанию

PHP код:
<?php

######################################## #######################################
#
#    показывает, во сколько раз различаются скорости 2х кусков кода
#    полезно для оптимизации
#    настройки:

    
$tests  5;    # количество тестов для среднего арифметического
    
$calc   50;   # количество измерений за 1 тест
    
$repeat 500;  # количество повторов(подряд) кода за 1 измерение

#    если используется ~ быстрый код, цифры нужно увеличить ( > )
#    если используется медленный код, цифры нужно уменьшить ( < )
#
######################################## #######################################

echo "BEGIN TEST\n\n";

$tests   abs( (int)$tests );
$calc    abs( (int)$calc );
$repeat  abs( (int)$repeat );
$control calc_control$repeat );
$cnt     $tests $calc;
$c       0;
$result  0;

if ( 
$tests === || $cnt === end_test"FATAL ERROR" );

for ( 
$i 0$i $tests; ++$i $result += test();
$result round$result $tests );

     if ( 
$result )    end_test"1 faster x{$result});
else if ( 
$result )    end_test"2 faster x" . (-$result) );
else            
end_test"~ identical" );

######################################## #######################################

function end_test$text )
{
    echo 
"\n\n {$text}\n\nEND TEST\nPress ENTER";
    die( 
fgets(STDIN) );
}
//eof

function calc_control$repeat )
{
    
$avg 0;
    
$cnt 5;
    for ( 
$i 0$i $cnt; ++$i )
    {
        
$time_s microtime(1);
        for ( 
$o 0$o $repeat; ++$o ) {}
        
$time_f microtime(1);
        
$avg += $time_f $time_s;
    }
    return( 
$avg/$cnt );
}

function 
test()
{
    global 
$calc$repeat$control$cnt$c;
    
$f1  0;
    
$f2  0;
    
$result 0;
    for ( 
$i 0$i $calc; ++$i )
    {
        
$time_s microtime(1); for ( $o 0$o $repeat; ++$o ) {}
        
$time_f microtime(1); 10000 * ( $time_f $time_s $control );

        
$time_s microtime(1);
        for ( 
$o 0$o $repeat; ++$o )
        {






            
# код 1
            
microtime();






        }
        
$time_f microtime(1);
        
$timer1 10000 * ( $time_f $time_s $control );

        
$time_s microtime(1); for ( $o 0$o $repeat; ++$o ) {}
        
$time_f microtime(1); 10000 * ( $time_f $time_s $control );

        
$time_s microtime(1);
        for ( 
$o 0$o $repeat; ++$o )
        {






            
# код 2
            
microtime(1);






        }
        
$time_f microtime(1);
        
$timer2 10000 * ( $time_f $time_s $control );

        
$c++;  $complete round( ($c/$cnt) * 100 );
        echo 
"\r complete: {$complete}%";

        if ( 
round$timer1 ) === round$timer2 ) )
        {
            
end_test("absolute identical");
        }
        else if ( 
$timer2 $timer1 // 1 faster
        
{
            ++
$f1;
            
$result += $timer2 $timer1;
        }
        else if ( 
$timer1 $timer2 // 2 faster
        
{
            ++
$f2;
            
$result += $timer1 $timer2;
        }
    }
    
usleep(100000);
    
$result /= $calc;
    if ( 
$f1>&& $f2>)    return 0;
    else if ( 
$f1 )    return $result;
    else            return -
$result;
}
//eof

######################################## #######################################

?>
 
Ответить с цитированием