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

开发 后端
PHP递归效率比正常的需求要低将近3倍的差距,那么我们如何才能在编码中来提高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. } 

 

#t#在我的IIS 服务器上(CGI)模式,我的PHP递归效率测试结果是:

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

 

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

 

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

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

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

责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-01 15:48:12

提高PHP运行效率

2009-11-30 18:59:52

PHP数组排序

2009-11-23 10:31:25

PHP使用JSON

2009-12-02 10:32:02

PHP语法解析函数

2023-04-03 16:21:20

数字孪生数字建筑

2015-04-16 10:15:45

PHPPHP执行效率PHP技巧

2023-12-29 15:24:56

物联网通信能源管理

2021-04-27 06:52:49

团队研发效率

2020-04-30 16:15:04

物联网IoT工业效率

2017-09-29 10:38:30

Android

2021-07-17 06:48:09

AI人工智能

2009-11-25 16:29:08

PHP删除数组元素

2011-05-30 13:28:00

PHP

2020-09-09 11:23:22

数据科学与分析

2024-03-25 11:42:50

2014-02-28 10:26:16

Linux文本搜索ack

2024-01-16 16:47:24

数字孪生建筑信息模型

2018-04-26 12:19:20

物联网IoT交通

2009-12-11 10:41:11

PHP变量解析顺序

2021-03-26 11:52:50

Debug效率运行
点赞
收藏

51CTO技术栈公众号