欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php记录程序执行时间microtime

程序员文章站 2024-01-30 21:22:10
...
/**
 * 记录程序程序时间---只能记录连续的调用改函数
 * @param		$time_key		记录的key,多次执行可以指定同一个key
 * @return		none				没有返回值,直接获取全局变量$time_arr,如果是在方法中调用需要先global一次
 **/
function showExecuTime($time_key = '') {
	global $time_arr,$time_start,$time_end,$time_num;
	$time_now = microtime(true);
	if ( !$time_num ) {
		$time_num = 0;
	}
	$time_num++;
	if ( $time_num%2 == 1 ) {
		$time_start = $time_now;
	} else {
		$time_end = $time_now;
	}
	if ( $time_num %2 == 0 && $time_start && $time_end ) {
		$time = $time_end - $time_start;
		if ( $time_key ) {
			$time_arr[$time_key] += $time;
		} else {
			$time_arr[] = $time;
		}
		$time_arr['total'] += $time;
	}
}

/**
 * 记录程序程序时间---可以多次交叉的调用
 * @param		$time_key		记录的key,多次执行可以指定同一个key
 * @return		none				没有返回值,直接获取全局变量$time_arr,如果是在方法中调用需要先global一次
 **/
function showExecuTime($time_key = '') {
	global $time_arr,$time_key_arr,$time_num;
	$time_now = microtime(true);
	if ( !$time_key ) {
		if ( !$time_num ) {
			$time_num = 0;
		}
		$time_num++;
		$time_key = floor(($time_num+1)/2) - 1;
	}
	// 记录本次是开始时间还是结束时间
	if ( !isset($time_key_arr[$time_key])) {
		$time_key_arr[$time_key]['num'] = 0;
	}
	$time_key_arr[$time_key]['num']++;
	if ( $time_key_arr[$time_key]['num']%2 == 1 ) {
		$time_key_arr[$time_key]['time_start'] = $time_now;
	} else {
		$time_key_arr[$time_key]['time_end'] = $time_now;
	}
	
	// 如果连续记录2次,计算结果
	if ( $time_key_arr[$time_key]['num'] %2 == 0 && $time_key_arr[$time_key]['time_start'] && $time_key_arr[$time_key]['time_end'] ) {
		$time = $time_key_arr[$time_key]['time_end'] - $time_key_arr[$time_key]['time_start'];
		$time_arr[$time_key] += $time;
		$time_arr['total'] += $time;
	}
}

showExecuTime('all');
showExecuTime();
sleep(1);
showExecuTime('all');//////
sleep(1);
showExecuTime();
sleep(1);
showExecuTime();
sleep(1);
showExecuTime();
showExecuTime('a');
sleep(1);
showExecuTime('a');
// showExecuTime('all');//////
print_r($time_arr);
print_r($time_key_arr);

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了php记录程序执行时间microtime,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

php记录程序执行时间microtime

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频