PHP kriter fonksiyonları zamanlayıcı komut

3 Cevap

Kimin yürütme zamanı ben karşılaştırmak istiyorum iki işlevi vardır:

function subject_one() {
    $str = preg_match_all( ...[SNIP]... );
    // ...[SNIP]...
    return $str;
}

function subject_two() {
    $reader = new XMLReader;
    $writer = new XMLWriter;
    // ...[SNIP]...
    echo $str;
}

Bunu yapmak için bir fonksiyon yazmak mümkün mü?

Örneğin:

function benchmark_two_functions( $first_function, $second_function ) {
    // do stuff
    return $length_of_time_to_complete_each_function
}

Ben Çoğu örnekler gördüm ben eğer mümkünse önlemek istiyorum script üst ve alt kod ekleyin.

3 Cevap

Bu deneyin

function subject_one(){
    sleep(1);
}

function subject_two(){
    sleep(5);
}

/* Result should be ~4 */
print benchmark_two_functions('subject_one','subject_two');

function getmicrotime() { 
    list($usec, $sec) = explode(" ",microtime()); 
    return ((float)$usec + (float)$sec);
}

function benchmark_two_functions($first_function, $second_function){
    $start = getmicrotime();
    $first_function();
    $exec_time_first = getmicrotime() - $start;

    $start = getmicrotime();
    $second_function();
    $exec_time_second = getmicrotime() - $start;

    return $exec_time_first - $exec_time_second;
}

PHP profilleme için PEAR has a Benchmark kütüphane. Bu oldukça iyi çalışıyor, ancak sunucuya root erişimi var ve bunu yükleyebilirsiniz eğer belki XDebug daha iyi bir araç.