Memory and time consumed by php test code execution

Source: Internet
Author: User
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:

  1. Echo memory_get_usage ();
  2. $ Var = str_repeat (www.phpfensi.com, 10000 );
  3. Echo memory_get_usage ();
  4. Unset ($ var );
  5. Echo memory_get_usage ();
  6. ?>

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:

  1. Function convert ($ size ){
  2. $ Unit = array ('B', 'KB', 'mb', 'GB', 'TB', 'petab ');
  3. Return @ round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2 ). ''. $ unit [$ I];
  4. }
  5. Echo convert (memory_get_usage (true ));
  6. ?>

265KB. The basic introduction of the two functions is complete. let's take a look at a test instance:

  1. $ T1 = microtime (true );
  2. $ M1 = memory_get_usage (true );
  3. Echo fixByte ($ m1 ).' ';
  4. /* When there are too many threads, there are too many threads, too many threads */
  5. /* When there are too many threads, there are too many threads, too many threads */
  6. $ T2 = microtime (true );
  7. $ M2 = memory_get_usage (true );
  8. Echo' '. FixByte ($ m2 ).' ';
  9. Echo' ';
  10. Echo 'time'. round ($ t2-$ t1), 4 ).' ';
  11. Echo 'mem '. fixByte ($ m2-$ m1 ).' ';
  12. /**
  13. * Format the byte as a proper value.
  14. * @ Param int $ byte bytes
  15. * @ Param string $ string the number of readable bytes formatted
  16. */
  17. Function fixByte ($ byte, $ string = true, $ dot_num = 9 ){
  18. $ Ret = array (
  19. 'Data' => $ byte,
  20. 'Danwei' => 'byte ',
  21. );
  22. If ($ byte <1024 ){
  23. } Else if ($ byte <1024*1024 ){
  24. $ Ret ['data'] = round ($ byte/1024, $ dot_num );
  25. $ Ret ['danwei'] = 'K ';
  26. } Else if ($ byte <1024*1024*1024 ){
  27. $ Ret ['data'] = round ($ byte/(1024*1024), $ dot_num );
  28. $ Ret ['danwei'] = 'M ';
  29. } Else if ($ byte <1024*1024*1024*1024 ){
  30. $ Ret ['data'] = round ($ byte/(1024*1024*1024), $ dot_num );
  31. $ Ret ['danwei'] = 'GB ';
  32. } Else if ($ byte <1024*1024*1024*1024*1024 ){
  33. $ Ret ['data'] = round ($ byte/(1024*1024*1024*1024), $ dot_num );
  34. $ Ret ['danwei'] = 'TB ';
  35. }
  36. If ($ string ){
  37. $ Ret = $ ret ['data']. ''. $ ret ['danwei'];
  38. }
  39. Return $ ret;
  40. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.