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. First
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 take a look at the usage of the microtime and memory_get_usage functions.
Meaning and usage
The microtime () function returns the current Unix timestamp and the number of microseconds.
Syntax:Microtime (get_as_float), 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.
The code example is as follows:Echo (microtime ());
Output:0.25139300 1138197510
1. 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. obtain the current memory consumption. the code is as follows:
- Echo memory_get_usage ();
- $ Var = str_repeat (www.phpfensi.com, 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. format memory_get_usage () and output the result 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. The basic introduction of the two functions is complete. let's take a look at a test instance:
- $ 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;
- }