PHP recursive efficiency analysis. In addition, it is 3 times less efficient. Therefore, you must be careful with recursion in PHP. Recently I wrote a fast sorting algorithm, and found that the recursive efficiency in PHP cannot be a one-size-fits-all algorithm, which is three times less efficient in a variety of different ways. Therefore, you must be careful with recursion in PHP.
I recently wrote a fast sorting algorithm and found that the recursive efficiency in PHP cannot be one-size-fits-all. it may be different on different servers.
The code is as follows:
Function qsort (& $ arr)
{
_ Quick_sort ($ arr, 0, count ($ arr)-1 );
}
/**
* Uses recursive algorithms for fast sorting.
*
* @ Param array $ array to be sorted by arr
* @ Param int $ low: Lowest sort sub-segment
* @ Param int $ highest sorting field
*/
Function _ quick_sort (& $ arr, $ low, $ high)
{
$ Low_data = $ arr [$ low];
$ Prev_low = $ low;
$ Prev_high = $ high;
While ($ low <$ high)
{
While ($ arr [$ high] >=$ low_data & $ low <$ high ){
$ High --;
}
If ($ low <$ high ){
$ Arr [$ low] = $ arr [$ high];
$ Low ++;
}
While ($ arr [$ low] <= $ low_data & $ low <$ high ){
$ Low ++;
}
If ($ low <$ high ){
$ Arr [$ high] = $ arr [$ low];
$ High --;
}
}
$ Arr [$ low] = $ low_data;
If ($ prev_low <$ low ){
_ Quick_sort ($ arr, $ prev_low, $ low );
}
If ($ low + 1 <$ prev_high ){
_ Quick_sort ($ arr, $ low + 1, $ prev_high );
}
}
Function quick_sort (& $ arr)
{
$ Stack = array ();
Array_push ($ stack, 0 );
Array_push ($ stack, count ($ arr)-1 );
While (! Empty ($ stack )){
$ High = array_pop ($ stack );
$ Low = array_pop ($ stack );
$ Low_data = $ arr [$ low];
$ Prev_low = $ low;
$ Prev_high = $ high;
While ($ low <$ high)
{
While ($ arr [$ high] >=$ low_data & $ low <$ high ){
$ High --;
}
If ($ low <$ high ){
$ Arr [$ low] = $ arr [$ high];
$ Low ++;
}
While ($ arr [$ low] <= $ low_data & $ low <$ high ){
$ Low ++;
}
If ($ low <$ high ){
$ Arr [$ high] = $ arr [$ low];
$ High --;
}
}
$ Arr [$ low] = $ low_data;
If ($ prev_low <$ low ){
Array_push ($ stack, $ prev_low );
Array_push ($ stack, $ low );
}
If ($ low + 1 <$ prev_high ){
Array_push ($ stack, $ low + 1 );
Array_push ($ stack, $ prev_high );
}
}
}
The code for testing speed is as follows:
The code is as follows:
Function qsort_test1 ()
{
$ Arr = range (1, 1000 );
Shuffle ($ arr );
$ Arr2 = $ arr;
$ T1 = microtime (true );
Quick_sort ($ arr2 );
$ T2 = microtime (true)-$ t1;
Echo "cost of non-recursive calls:". $ t2. "\ n ";
$ Arr1 = $ arr;
$ T1 = microtime (true );
Qsort ($ arr1 );
$ T2 = microtime (true)-$ t1;
Echo "recursive call cost:". $ t2. "\ n ";
}
On my IIS server (CGI) mode, my test result is:
Cost of non-recursive calls: 0.036401009559631
Recursive call cost: 0.053439617156982
On my Apache server, my test result is:
Cost of non-recursive calls: 0.022789001464844
Recursive call cost: 0.014809131622314
The results are the opposite, while the PHP version is the same.
It seems that the efficiency of recursion needs to be analyzed in detail.
Bytes. Therefore, you must be careful with recursion in PHP. Recently I wrote a fast sorting algorithm and found that the recursive efficiency in PHP cannot be one-size-fits-all...