Memory and time consumed by php test code execution. To test the memory and time consumed by code execution in php, we can directly use the two functions, microtime and memory_get_usage. I will introduce them to you. Let's first test the memory and time consumed by code execution in php. we can directly use the two functions, microtime and memory_get_usage. next I will introduce them to you.
Let's take a look at the usage of the microtime and memory_get_usage functions.
Meaning and usage
Microtime ()The function returns the current Unix timestamp and the number of microseconds.
Syntax
Microtime (get_as_float) parameter description
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: |
|
Echo (microtime ());
?> |
Output:
0.25139300 1138197510
I. function prototype
Int memory_get_usage ([bool $ real_usage = false])
2. version compatibility
PHP 4> = 4.3.2, PHP 5
III. basic usage and examples
1. get the current memory consumption
| The code is as follows: |
|
|
Echo memory_get_usage ();
$ Var = str_repeat (www. bKjia. c0m, 10000 );
Echo memory_get_usage ();
Unset ($ var );
Echo memory_get_usage ();
?> |
Result: 62328 122504 62416
Note: The value output by the memory_get_usage () function is bytes.
2. the result of formatting memory_get_usage () is output in KB.
| The code is as follows: |
|
Function convert ($ size ){
$ Unit = array ('B', 'KB', 'mb', 'GB', 'TB', 'petab ');
Return @ round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2 ). ''. $ unit [$ I];
}
Echo convert (memory_get_usage (true ));
?> |
265KB
Now we have finished introducing the two functions. let's take a look at a test instance.
| The code is as follows: |
|
|
$ T1 = microtime (true );
$ M1 = memory_get_usage (true );
Echo fixByte ($ m1 ).'
';
/* When there are too many threads, there are too many threads, too many threads */
/* When there are too many threads, there are too many threads, too many threads */
$ T2 = microtime (true );
$ M2 = memory_get_usage (true );
Echo'
'. FixByte ($ m2 ).'
';
Echo' ';
Echo 'time'. round ($ t2-$ t1), 4 ).'
';
Echo 'mem '. fixByte ($ m2-$ m1 ).'
';
/**
* Format the byte as a proper value.
* @ Param int $ byte bytes
* @ Param string $ string the number of readable bytes formatted
*/
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;
} |
Refer and memory_get_usage. I will introduce them to you. Let's start...