PHP性能测试代码

来源:https://blog.csdn.net/qq_41049126/article/details/89360658

function xn_start(){
    global $xn_start_time;
    global $xn_start_memory;
    /**
     * 代码性能测试代码
     */
    $xn_start_memory = memory_get_usage();                 //开始内存
    $xn_start_time = microtime(true);
}


function xn_end(){
    global $xn_start_time;
    global $xn_start_memory;
    echo '开始内存:' . $xn_start_memory . '<br>';

    $end_time = microtime(true);                        //获取程序执行结束的时间
    $run_time = ($end_time - $xn_start_time);       //计算差值 秒
    echo "[页面执行时间:{$run_time}]秒<br>";
    $end_memory = memory_get_usage();
    echo '运行后内存:'. $end_memory . '<br>';

    echo '使用的内存:' . ($end_memory - $xn_start_memory) . '<br>';
    echo '回到正常内存:'.memory_get_usage();


    die;
}