Home > Backend Development > PHP Tutorial > array_multisort和usort性能有高低吗?

array_multisort和usort性能有高低吗?

WBOY
Release: 2016-06-06 20:33:24
Original
1966 people have browsed it

在处理二维数组或多维数组排序上分别使用array_multisort和usort这两个函数之间有性能上的差异吗?

回复内容:

在处理二维数组或多维数组排序上分别使用array_multisort和usort这两个函数之间有性能上的差异吗?

在stackoverflow上看到的.另外 usort在比较同值排序的时候会出现排序随机状态,做榜单类数据不合理。

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 expect, 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...

经过测试:
arraycount=1000
usort排序执行时间为:0.034267902374268 s
array_multisort排序执行时间为:0.0045900344848633 s

arraycount=10000
usort排序执行时间为:0.29996109008789 s
array_multisort排序执行时间为:0.03429102897644 s

arratcount=100000
usort排序执行时间为:3.8284521102905 s
array_multisort排序执行时间为:0.85004997253418 s

可以看出array_multisort的确要快很多,但对于数据量很小的排序usort可以做到更为灵活的业务处理。

array_multisort常用的应用场景是对二维数组按列排序, 可以在二维数组中实现类似Mysql中 ORDER BY COLUMN的操作。

比如说:$knowledgeArr是一个二维数组,数组中的每个单元类似于数据库的一条记录,我们想把下面三个单元按照

每个单元里的level值来倒序排列。 这非常像数据库的ORDER BY ,但是怎么对二维数组进行ORDER BY level DESC的操作呢, 这就用到了PHP中array_multisort() 这个函数

<code>$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] => 语言文字运用
                            [knowledge_id] => 1
                            [level] => 1
                            [grade] => 1
                            [max_level] => 6 )
               );
</code>
Copy after login

现在有了包含行的数组$knowledgeArr,但是 array_multisort() 还需要一个包含单列level的数组,因此用以下代码来取得列,然后排序。

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

关于array_multisort()更详细的说明参考官方手册 http://cn2.php.net/array_multisort

至于性能, 因为usort使用的是你自己定义的PHP函数, 而array_multisort的排序是在PHP源码层实现的,所以在排序速度上应该是array_multisort来得更快一些。

亲测过,array_mutlisort更快!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template