在unixoid系统上(以及在Windows上的php 7+中),您可以使用getrusage,例如:

//   start
$rustart = getrusage();

// Code ...

//   end
function rutime($ru, $rus, $index) {
    return ($ru[\"ru_$index.tv_sec\"]*1000 + intval($ru[\"ru_$index.tv_usec\"]/1000))
     -  ($rus[\"ru_$index.tv_sec\"]*1000 + intval($rus[\"ru_$index.tv_usec\"]/1000));
}

$ru = getrusage();
echo \"This process used \" . rutime($ru, $rustart, \"utime\") .
    \" ms for its computations\\n\";
echo \"It spent \" . rutime($ru, $rustart, \"stime\") .
    \" ms in system calls\\n\";

请注意,如果要为每个测试生成一个php实例,则无需计算差异。

 

如果您只需要挂钟时间而不是CPU执行时间,那么计算起来很简单:

//place this before any   you want to calculate time
$time_start = microtime(true); 

//sample  
for($i=0; $i<1000; $i++){
 //do anything
}

$time_end = microtime(true);

//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time = ($time_end - $time_start)/60;

//execution time of the  
echo \'<b>Total Execution Time:</b> \'.$execution_time.\' Mins\';
// if you get weird results, use number_format((float) $execution_time, 10) 

请注意,这将包括PHP等待外部资源(如磁盘或数据库)的时间,这些资源不用于max_execution_time。

 

最简单的方法:

<?php

$time1 = microtime(true);

//  code
//...

$time2 = microtime(true);
echo \'  execution time: \' . ($time2 - $time1); //value in seconds
收藏 打印