PHP头条
热点:

探讨应当如何提高PHP递归效率


我们在实际代码编程中,会发现PHP递归效率是非常低下的,对于程序员来说,他们必须要很好的处理PHP的递归。在这篇文章中我们具体向大家介绍了PHP递归效率的提高方法,希望对又需要的朋友有所帮助。

最近写了一个快速排序的算法,发现PHP中的递归效率不能一刀切,在各种不同的服务器中,可能会表现不一样。

  1. function qsort(&$arr)  
  2. {  
  3. _quick_sort($arr, 0, count($arr) - 1);  
  4. }  
  5.  
  6. /**  
  7. * 采用递归算法的快速排序。  
  8. *  
  9. * @param array $arr 要排序的数组  
  10. * @param int $low 最低的排序子段  
  11. * @param int $high 最高的排序字段  
  12. */  
  13. function _quick_sort(&$arr, $low, $high)  
  14. {  
  15. $low_data = $arr[$low];  
  16. $prev_low = $low;  
  17. $prev_high = $high;  
  18. while ($low < $high)   
  19. {  
  20. while ($arr[$high] >= $low_data && $low < $high) {  
  21. $high--;  
  22. }  
  23. if ($low < $high) {  
  24. $arr[$low] = $arr[$high];  
  25. $low++;  
  26. }  
  27. while ($arr[$low] <= $low_data && $low < $high) {  
  28. $low++;  
  29. }  
  30. if ($low < $high) {  
  31. $arr[$high] = $arr[$low];  
  32. $high--;  
  33. }  
  34. }  
  35. $arr[$low] = $low_data;  
  36. if ($prev_low < $low) {  
  37. _quick_sort($arr, $prev_low, $low);  
  38. }  
  39. if ($low + 1 < $prev_high) {  
  40. _quick_sort($arr, $low + 1, $prev_high);  
  41. }  
  42. }  
  43.  
  44. function quick_sort(&$arr)  
  45. {  
  46. $stack = array();  
  47. array_push($stack, 0);  
  48. array_push($stack, count($arr) -1);  
  49. while (!empty($stack)) {  
  50. $high = array_pop($stack);  
  51. $low = array_pop($stack);  
  52. $low_data = $arr[$low];  
  53. $prev_low = $low;  
  54. $prev_high = $high;  
  55. while ($low < $high)   
  56. {  
  57. while ($arr[$high] >= $low_data && $low < $high) {  
  58. $high--;  
  59. }  
  60. if ($low < $high) {  
  61. $arr[$low] = $arr[$high];  
  62. $low++;  
  63. }  
  64. while ($arr[$low] <= $low_data && $low < $high) {  
  65. $low++;  
  66. }  
  67. if ($low < $high) {  
  68. $arr[$high] = $arr[$low];  
  69. $high--;  
  70. }  
  71. }  
  72. $arr[$low] = $low_data;  
  73. if ($prev_low < $low) {  
  74. array_push($stack, $prev_low);  
  75. array_push($stack, $low);  
  76. }  
  77. if ($low + 1 < $prev_high) {  
  78. array_push($stack, $low + 1);  
  79. array_push($stack, $prev_high);  
  80. }  
  81. }  

下面是PHP递归效率测试速度的代码:

  1. function qsort_test1()  
  2. {  
  3. $arr = range(1, 1000);  
  4. shuffle($arr);  
  5. $arr2 = $arr;  
  6. $t1 = microtime(true);  
  7. quick_sort($arr2);  
  8. $t2 = microtime(true) - $t1;  
  9. echo "非递归调用的花费:" . $t2 . "\n";  
  10. $arr1 = $arr;  
  11. $t1 = microtime(true);  
  12. qsort($arr1);  
  13. $t2 = microtime(true) - $t1;  
  14. echo "递归调用的花费:" . $t2 . "\n";  
  15. } 

  • PHP函数stristr()的具体使用方式介绍
  • PHP代码性能优化的技巧讲解
  • 如何运用PHP函数preg_match_all测试正则
  • 如何使用PHP运算符==比较字符串
  • PHP静态变量static的示例代码演示
在我的IIS 服务器上CGI)模式,我的PHP递归效率测试结果是:

非递归调用的花费:0.036401009559631
递归调用的花费:0.053439617156982

在我的Apache 服务器上,我的测试结果是:

非递归调用的花费:0.022789001464844
递归调用的花费:0.014809131622314

PHP递归效率的结果完全相反,而PHP的版本是一样的。

看来对PHP递归效率要具体问题具体分析了。

www.phpzy.comtrue/php/14506.htmlTechArticle探讨应当如何提高PHP递归效率 我们在实际代码编程中,会发现PHP递归效率是非常低下的,对于程序员来说,他们必须要很好的处理PHP的递归。在这篇文章中我们具体向大家介绍了PHP递归...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐