Table of Contents
回复内容:
Home Backend Development PHP Tutorial array_multisort和usort性能有高低吗?

array_multisort和usort性能有高低吗?

Jun 06, 2016 pm 08:33 PM
php sort

在处理二维数组或多维数组排序上分别使用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更快!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles