Home  >  Article  >  Backend Development  >  Memory and time consumed by php test code execution_PHP tutorial

Memory and time consumed by php test code execution_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:12757browse

To test the memory and time consumed by code execution in PHP, we can directly use two functions, microtime and memory_get_usage. Let me introduce them to you below.

Let’s take a look at the usage of microtime and memory_get_usage functions first

Yihe Usage

microtime() The function returns the current Unix timestamp and microseconds.

Grammar

microtime(get_as_float) parameter description
get_as_float If the get_as_float parameter is given and its value is equivalent to TRUE, this function returns a floating point number.

Example

The code is as follows Copy code
代码如下 复制代码
echo(microtime());
?>
echo(microtime());

?>

Output:

0.25139300 1138197510

1. Function prototype

int memory_get_usage ([ bool $real_usage=false ] )

Two, version compatible

PHP 4 >= 4.3.2,PHP 5

3. Basic usage and examples

1, get the current memory consumption
 代码如下 复制代码

echo memory_get_usage();
$var=str_repeat(www.bKjia.c0m,10000);
echo memory_get_usage();
unset($var);
echo memory_get_usage();
?>

The code is as follows Copy code

echo memory_get_usage();

$var=str_repeat(www.bKjia.c0m,10000);

echo memory_get_usage();

unset($var);

echo memory_get_usage();

?>

 代码如下 复制代码
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));
?>
Result output: 62328 122504 62416 Note: The value output by the memory_get_usage() function is in bytes unit 2, format the memory_get_usage() result and output it in KB units
The code is as follows Copy code
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

Now that the two functions have been basically introduced, let me look at a test example

The code is as follows
 代码如下 复制代码

$t1 = microtime(true);
$m1 = memory_get_usage(true);
echo fixByte($m1). '
';

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

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

$t2 = microtime(true);
$m2 = memory_get_usage(true);
echo '
' . fixByte($m2). '
';

echo '


';
echo 'time ' . round(($t2 - $t1), 4) .'
';
echo 'mem ' . fixByte($m2 - $m1) .  '
';

/**
 * 格式化字节为合适的数值
 * @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;
}

Copy code
$t1 = microtime(true);
$m1 = memory_get_usage(true);
echo fixByte($m1). '
';

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/ $t2 = microtime(true);
$m2 = memory_get_usage(true);
echo '
' . fixByte($m2). '
';
echo '
';
echo 'time ' . round(($t2 - $t1), 4) .'
';
echo 'mem ' . fixByte($m2 - $m1) . '
';
/**
* Format bytes to appropriate values
* @param int $byte number of bytes
* @param string $string formatted number of bytes that are highly readable
​*/
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;
} http://www.bkjia.com/PHPjc/631551.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631551.htmlTechArticleTo test the memory and time consumed by code execution in php, we can directly use two functions, microtime and memory_get_usage. Okay, let me introduce it to you. Let’s go first...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn