教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php测试代码执行消耗的内存和时间

php测试代码执行消耗的内存和时间

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供php测试代码执行消耗的内存和时间等资源,欢迎您收藏本站,我们将为您提供最新的php测试代码执行消耗的内存和时间资源
在php中要测试代码执行消耗的内存和时间我们可以直接使用俩函数,microtime 和 memory_get_usage就可以了,下面我来给大家介绍介绍。

我们先来看看microtime 和 memory_get_usage函数用法吧

义和用法

microtime() 函数返回当前 Unix 时间戳和微秒数。

语法

microtime(get_as_float)参数 描述
get_as_float 如果给出了 get_as_float 参数并且其值等价于 TRUE,该函数将返回一个浮点数。

例子

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7949')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7949><?php
echo(microtime());
?>

输出:

0.25139300 1138197510

一,函数原型

int memory_get_usage ([ bool $real_usage=false ] )

二,版本兼容

PHP 4 >= 4.3.2,PHP 5

三,基础用法与实例

1,获取当前的内存消耗量

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4261')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4261>

<?php
echo memory_get_usage();
$var=str_repeat(www.jiaochengji.com,10000);
echo memory_get_usage();
unset($var);
echo memory_get_usage();
?>

结果输出:62328 122504 62416

说明:memory_get_usage() 函数输出的数值为 bytes 单位

2,格式化 memory_get_usage() 结果以 KB 为单位输出

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6643')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6643><?php
function convert($size){
 $unit=array('b','kb','mb','gb','tb','pb');
 return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
echo convert(memory_get_usage(true));
?>

265KB

好了两个函数基本介绍完了,下面我来看一个测试实例

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8799')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8799>

<?php

$t1 = microtime(true);
$m1 = memory_get_usage(true);
echo fixByte($m1). '<br />';

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/

$t2 = microtime(true);
$m2 = memory_get_usage(true);
echo '<br />' . fixByte($m2). '<br />';

echo '<hr >';
echo 'time ' . round(($t2 - $t1), 4) .'<br />';
echo 'mem ' . fixByte($m2 - $m1) .  '<br />';

/**
 * 格式化字节为合适的数值
 * @param int $byte 字节数
 * @param string $string 格式化的可读性强的字节数
 */
function fixByte($byte, $string = true, $dot_num = 9) {
 $ret = array(
   'data'=>$byte,
   'danwei'=>'Byte',
 );

 if ($byte < 1024) {

 } else if ($byte < 1024*1024) {
  $ret['data'] = round($byte / 1024, $dot_num);
  $ret['danwei']='K';
 } else if ($byte < 1024*1024*1024) {
  $ret['data'] = round($byte / (1024*1024), $dot_num);
  $ret['danwei']='M';
 } else if ($byte < 1024*1024*1024*1024) {
  $ret['data'] = round($byte / (1024*1024*1024), $dot_num);
  $ret['danwei']='GB';
 } else if ($byte < 1024*1024*1024*1024*1024) {
  $ret['data'] = round($byte / (1024*1024*1024*1024), $dot_num);
  $ret['danwei']='TB';
 }

 if ($string) {
  $ret = $ret['data'] . ' ' . $ret['danwei'];
 }

 return $ret;
}

您可能感兴趣的文章:
opcache PHP新的字节码缓存扩展详解
Mysql insert语句的优化总结
php和golang怎么配合
golang 性能测试 (1)
Ajax异步请求所耗时间比较
PHP代码优化的方法介绍(图文)
C# 泛型集合List与非泛型集合ArrayList之性能比较
提高php运行效率的50个实用技巧
PHP代码优化之array_push
一种改进的轻量级.NET应用程序性能测试框架

[关闭]
~ ~