are Array_multisort and usort performance High or low?

Source: Internet
Author: User
Are there any performance differences between the two functions that handle two-dimensional arrays or multidimensional array sorting, using Array_multisort and Usort respectively?

Reply content:

Are there any performance differences between the two functions that handle two-dimensional arrays or multidimensional array sorting, using Array_multisort and Usort respectively?

Seen on the StackOverflow. In addition Usort in the comparison of the same value of the time will appear in the sort of random state, Do List class 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 the representative data rows, Usort () was 7-15x slow ER than Array_multisort () when the column is random values of type int and the column was pre-extracted. That's as one might expect, since for every comparison "re comparing an entire PHP function call to optimized Intrinsi C code.

Using a 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, if you start getting up to 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
usort Sort Execution time is: 0.034267902374268 s
array_multisort Sort Execution time is: 0.0045900344848633 s

arraycount=10000
usort Sort Execution time is: 0.29996109008789 s
array_multisort Sort Execution time is: 0.03429102897644 s

arratcount=100000
usort Sort Execution time is: 3.8284521102905 s
array_multisort Sort Execution time is: 0.85004997253418 s

It can be seen that Array_multisort is indeed much faster, but for a small sort of data usort can be more flexible business processing.

The common application scenario for Array_multisort is to sort the two-dimensional arrays by column, which can be implemented in a two-dimensional array similar to the order by column in MySQL.

For example: $KNOWLEDGEARR is a two-dimensional array, each cell in the array is similar to a record in the database, we want to put the following three units according to

Level values in each cell are sorted in reverse order. This is very much like the order by of the database, but how to do the order by level DESC of the two-dimensional array, which is used in PHP array_multisort () This function

 $KNOWLEDGEARR = Array ([0] = = Array ( [Name] = [KNOWLEDGE_ID] = 2 [level] + 2 [Grade] = 2 [Max_level] = 4), [1] = = Array ([name] + = Glyph [ KNOWLEDGE_ID] = 3 [level] + 1 [grade] + 2 [Max_level] = 4), [2] = = Array ( [Name] + + language use [knowledge_id] = 1 [l EVEL] = 1 [Grade] = 1 [max_level] + 6)); 

Now that you have the array $knowledgearr that contains the rows, but Array_multisort () also needs an array that contains a single-column level, so use the following code to get the columns, and then sort them.

    $sortCol = array();    foreach($knowledgeArr as $val) {        $sortCol[] = $val['level'];    }  array_multisort($sortCol, SORT_DESC, $knowledgeArr);//以知识点的level倒序排列

For more detailed instructions on Array_multisort () refer to the official manual Http://cn2.php.net/array_multisort

As for performance, because Usort uses your own defined PHP functions, and Array_multisort sort is implemented in the PHP source layer, it should be array_multisort faster to sort the speed.

Pro-Test, array_mutlisort faster!

  • Related Article

    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.