PHP运行时间与内存消耗统计代码

使用很简单,把代码添加在PHP文件头部,运行PHP时就会echo出相关信息
在这里插入图片描述
在这里插入图片描述
想修改呈现的方式 echo 'PHP:总内存「' . $neicun_zong_out . '」' . ' ' . '消耗内存「' . $neicun_yong_out . '」' . ' ' . '耗时「' . $time . '」'; 修改此段即可。

<?php
/* PHP运行时间&内存消耗统计工具
 * 精确度:
 * 	不足24小时:0.01秒(10毫秒)~24小时
 * 	超过24小时:1秒~数天
 */
// 开始的时钟时间(秒)
$huafei_time_start = microtime(true);
function changeTimeType() {
	// 结束的时钟时间(秒)
	$huafei_time_end = microtime(true);
	// 计算脚本执行时间
	$seconds = ($huafei_time_end - $GLOBALS['huafei_time_start']);
	if ($seconds > 3600) {
		$hours = intval($seconds/3600);
		$minutes = $seconds % 3600;
		$time = $hours."小时".gmstrftime('%M分钟%S秒', $minutes);
	} else {
		$jingduS = mb_substr($seconds,stripos($seconds,'.'),3);
		$time = gmstrftime('%H小时%M分钟%S' . $jingduS . '秒', $seconds);
		if(mb_substr($time,0,2) == '00') {
			$time = gmstrftime('%M分钟%S' . $jingduS . '秒', $seconds);
		}
		if(mb_substr($time,0,2) == '00') {
			//诡异的if语句,因为$time在上一个if语句被修改了所以此处不是mb_substr($time,4,2),而是mb_substr($time,0,2)
			$time = gmstrftime('%S' . $jingduS . '秒', $seconds);
		}
		if(mb_substr($time,0,2) == '00') {
			$time = gmstrftime('0' . $jingduS . '秒', $seconds);
			if($jingduS == '0') {
				$time = '0秒';
			}
		}
	}
	//分配给PHP的总内存
	$neicun_zong = memory_get_usage(true);
	$neicun_zong_kb = $neicun_zong / 1024;
	$neicun_zong_out = $neicun_zong_kb . ' ' . 'KB';
	if($neicun_zong_kb > 10240) {
		$neicun_zong_mb = $neicun_zong_kb / 1024;
		$neicun_zong_out = $neicun_zong_mb . ' ' . 'MB';
	}
	if($neicun_zong_mb > 10240) {
		$neicun_zong_gb = $neicun_zong_mb / 1024;
		$neicun_zong_out = $neicun_zong_gb . ' ' . 'GB';
	}
	//PHP实际使用的内存
	$neicun_yong = memory_get_usage(false);
	$neicun_yong_kb = $neicun_yong / 1024;
	$neicun_yong_out = $neicun_yong_kb . ' ' . 'KB';
	if($neicun_yong_kb > 10240) {
		$neicun_yong_mb = $neicun_yong_kb / 1024;
		$neicun_yong_out = $neicun_yong_mb . ' ' . 'MB';
	}
	if($neicun_yong_mb > 10240) {
		$neicun_yong_gb = $neicun_yong_mb / 1024;
		$neicun_yong_out = $neicun_yong_gb . ' ' . 'GB';
	}
	echo 'PHP:总内存「' . $neicun_zong_out . '」' . ' ' . '消耗内存「' . $neicun_yong_out . '」' . ' ' . '耗时「' . $time . '」';
}
register_shutdown_function("changeTimeType");
/*PHP运行时间&内存消耗统计结束*/

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deng_Xian_Shemg

捐助1元钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值