本文介绍了如何找到php执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站中有一大段 PHP 代码,我想知道处理的执行时间.我该怎么做?

I have a large PHP code in my website, I want to know the execution time of processing. How can I do this?

<?php
// large code
// large code
// large code

// print execution time here
?>

推荐答案

您可以使用 microtime 作为 PHP 的开始结束代码:

You can use microtime as the start and end of your PHP code:

<?php
    $time_start = microtime(true);
    sleep(1);
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "Process Time: {$time}";
    // Process Time: 1.0000340938568
?>

自 PHP 5.4.0 开始,无需获取开始时间,$_SERVER 超全局数组已经有了它:

As of PHP 5.4.0, there is no need to get start time at the beginning,the $_SERVER superglobal array already has it:

<?php
    sleep(1);
    $time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
    echo "Process Time: {$time}";
    // Process Time: 1.0061590671539
?>

这篇关于如何找到php执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:15