欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

PHP检测每一段代码执行时间

shiping1 的头像
<?php
// 实例1

/**
 * @start time
 */
function proStartTime() {
    global $startTime;
    $mtime1 = explode(" ", microtime());
    $startTime = $mtime1[1] + $mtime1[0];
}

/**
 * @End time
 */
function proEndTime() {
    global $startTime,$set;
    $mtime2 = explode(" ", microtime());
    $endtime = $mtime2[1] + $mtime2[0];
    $totaltime = ($endtime - $startTime);
    $totaltime = number_format($totaltime, 7);
    echo "<br/>process time: ".$totaltime;
}

// 程序调用开始记时
proStartTime();

sleep(1);     // sleep() 延时代码执行若干秒
proEndTime(); // 程序在每一段所消耗的执行时间
sleep(2);
proEndTime();
sleep(3);
proEndTime(); 


/************************************************* 华丽的分割线 **************************************************/

// 实例2
$t1 = microtime(true);
sleep(3);
$t2 = microtime(true);
echo '程序耗时'.round($t2-$t1,3).'秒';
?>

来自 http://www.verydemo.com/demo_c116_i55569.html

普通分类: