Is there a performance difference between array_multisort and usort functions used to process two-dimensional array or multi-dimensional array sorting? Is there a performance difference between array_multisort and usort functions used to process two-dimensional array or multi-dimensional array sorting?
Reply content:
Is there a performance difference between array_multisort and usort functions used to process two-dimensional array or multi-dimensional array sorting?
In stackoverflow, we can see that, in addition, when the usort is compared to the same value for sorting, the sorting is random, and the list data is unreasonable.
Usort () is more concise and doesn't require extracting a column array to feed to array_multisort (). (It also does less than array_multisort .)
However, when I repeatedly tested it today on arrays of 20,000 and 10,000 representative data rows, usort () was 7-15x slower than array_multisort () when the column was random values of type int and the column was pre-extracted. that is as one might failed CT, since for every comparison you're comparing an entire php function call to optimized intrinsic code.
Using an anonymous function as in the previous reply gave a 30-35% improvement over passing usort () the name of a defined function. it was never better than 8x slower and usually worse than 10x slower. often this won't matter, but when you start getting up into tenths of a second of CPU time just for sorting one array, it might. extracting the column without array_column () on a pre-5.5 server never quite halved the differential at best.
Http://stackoverflow.com/questions/3013974/php-usort-or-array-multisor...
Tested:
Arraycount = 1000
UsortSorting Execution time: 0.034267902374268 s
Array_multisortSorting Execution time: 0.0045900344848633 s
Arraycount = 10000
UsortSorting Execution time: 0.29996109008789 s
Array_multisortSorting Execution time: 0.03429102897644 s
Arratcount = 100000
UsortSorting Execution time: 3.8284521102905 s
Array_multisortSorting Execution time: 0.85004997253418 s
We can see thatArray_multisortIt is indeed much faster, but the sorting of a small amount of dataUsortIt can achieve more flexible business processing.
Array_multisort is commonly used to sort two-dimensional arrays by column. you can perform operations similar to order by column in Mysql in a two-dimensional array.
For example, $ knowledgeArr is a two-dimensional array. each unit in the array is similar to a record in the database.
The level values in each unit are arranged in reverse order. This is very similar to the order by function of the database. but how can we perform the order by level DESC operation on the two-dimensional array? this uses the array_multisort () function in PHP.
$ KnowledgeArr = array ([0] => array ([name] => [knowledge_id] => 2 [level] => 2 [grade] => 2 [max_level] => 4 ), [1] => array ([name] => [knowledge_id] => 3 [level] => 1 [grade] => 2 [max_level] => 4 ), [2] => array ([name] => use [knowledge_id] => 1 [level] => 1 [grade] => 1 [max_level] => 6) );
Now there is an array containing rows $ knowledgeArr, but array_multisort () also needs an array containing a single column level, so use the following code to retrieve the column and then sort it.
$ SortCol = array (); foreach ($ knowledgeArr as $ val) {$ sortCol [] = $ val ['level'];} array_multisort ($ sortCol, SORT_DESC, $ knowledgeArr); // rank the Knowledge Point in descending order.
For more detailed description of array_multisort (), refer to the official manual http://cn2.php.net/array_multisort
As for the performance, usort uses your own PHP function, while array_multisort is implemented in the PHP source code layer, so the sorting speed should be faster than array_multisort.
Tested by myself, array_mutlisort is faster!