用到的函数
计算当前时间戳的函数
/**
* 当前时间戳
* @return mixed|string
*/
function _runtime(){
$mtime = microtime();
$time = explode(' ', $mtime);
return $time[1]+$time[0];
}
格式化时间函数
function formatTime($time){
if(is_numeric($time)){
$value = array(
"years" => 0, "days" => 0, "hours" => 0,
"minutes" => 0, "seconds" => 0,
);
if($time >= 31556926){
$value["years"] = floor($time/31556926);
$time = ($time%31556926);
}
if($time >= 86400){
$value["days"] = floor($time/86400);
$time = ($time%86400);
}
if($time >= 3600){
$value["hours"] = floor($time/3600);
$time = ($time%3600);
}
if($time >= 60){
$value["minutes"] = floor($time/60);
$time = ($time%60);
}
$value["seconds"] = floor($time);
//return (array) $value;
$t="";
if(!empty($value['years'])){
$t.=$value["years"] ."年";
}
if(!empty($value["days"])){
$t.=$value['days']."天";
}
if(!empty($value["hours"])){
$t.=$value['hours']."小时";
}
if(!empty($value["minutes"])){
$t.=$value['minutes']."分";
}
$t.=$value["seconds"]."秒";
return $t;
}else{
return FALSE;
}
}
格式化字节函数
function formatSize($bytes) {
$units = [' B', ' KB', ' MB', ' GB', ' TB'];
for ($i = 0; $bytes >= 1024 && $i < 4; $i++) $bytes /= 1024;
return round($bytes, 2) . $units[$i];
}
计算程序消耗的时间
function expendTime($startTime){
return formatTime(_runtime() - $startTime);
}
计算程序消耗的内存
function expendMemory($startMemory){
return formatSize(memory_get_usage() - $startMemory);
}
计算内存峰值
function maxMemory()
{
return formatSize(memory_get_peak_usage());
}
在程序开头记录当前时间及内存
define('START_TIME', _runtime());
define('START_MEMORY', memory_get_usage());
程序结束后计算耗时及内存占用情况
echo '执行耗时:[ ' . expendTime(START_TIME) . ' ], 内存:[ ' . expendMemory(START_MEMORY) . ' ], 内存峰值:[ ' . maxMemory() . ' ]' . "\r\n";