PHP基准函数计时器脚本

PHP基准函数计时器脚本,php,Php,我想比较两个函数的执行时间: function subject_one() { $str = preg_match_all( ...[SNIP]... ); // ...[SNIP]... return $str; } function subject_two() { $reader = new XMLReader; $writer = new XMLWriter; // ...[SNIP]... echo $str; } 是否可以编写

我想比较两个函数的执行时间:

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

function subject_two() {
    $reader = new XMLReader;
    $writer = new XMLWriter;
    // ...[SNIP]...
    echo $str;
}
是否可以编写一个函数来执行此操作

例如:

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

我看到的大多数示例都会在脚本的顶部和底部添加代码,如果可能的话,我希望避免这样做。

也许可以使用勾号

PHP评测库。它工作得很好,但如果您具有服务器的root访问权限并可以安装它,它可能是一个更好的工具。

试试这个

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;
}

我想知道如何将函数
benchmark\u two\u functions()
修改为多次运行
subject\u one()
subject\u two()