在处理二维数组或多维数组排序上分别使用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>
现在有了包含行的数组$knowledgeArr,但是 array_multisort() 还需要一个包含单列level的数组,因此用以下代码来取得列,然后排序。
<code> $sortCol = array(); foreach($knowledgeArr as $val) { $sortCol[] = $val['level']; } array_multisort($sortCol, SORT_DESC, $knowledgeArr);//以知识点的level倒序排列 </code>
关于array_multisort()更详细的说明参考官方手册 http://cn2.php.net/array_multisort
至于性能, 因为usort使用的是你自己定义的PHP函数, 而array_multisort的排序是在PHP源码层实现的,所以在排序速度上应该是array_multisort来得更快一些。
亲测过,array_mutlisort更快!

tomakephpapplicationsfaster、followthesesteps:1)useopcodecachinglikeopcacheTostoredscriptbytecode.2)最小化abasequeriesecachingingindexing.3)leveragephp7機能forbettercodeefficiency.4)

依存性注入(DI)は、明示的に推移的な依存関係によりPHPコードのテスト可能性を大幅に改善します。 1)DI分離クラスと特定の実装により、テストとメンテナンスが柔軟になります。 2)3つのタイプのうち、コンストラクターは、状態を一貫性に保つために明示的な式依存性を注入します。 3)DIコンテナを使用して複雑な依存関係を管理し、コードの品質と開発効率を向上させます。

DatabaseQueryoptimizationInpholvesseveralstrategESTOEnhancePerformance.1)selectonlynlynlyndorycolumnStoredatedataTransfer.2)useindexingtospeedupdataretrieval.3)revenmecrycachingtostoreres sultsoffrequent queries.4)

phpisusededemingemailsduetoitsbuilt-inmail()functionandsupportiveLibrarieslikephpmailerandswiftmailer.1)usethemail()functionforbasicemails、butithaslimitations.2)emploadforadvancedfeatureSlikelikelivableabableabuses.3)雇用

PHPパフォーマンスボトルネックは、次の手順で解決できます。1)パフォーマンス分析にXdebugまたはBlackfireを使用して問題を見つける。 2)データベースクエリを最適化し、APCUなどのキャッシュを使用します。 3)array_filterなどの効率的な関数を使用して、配列操作を最適化します。 4)bytecodeキャッシュ用のopcacheを構成します。 5)HTTP要求の削減や写真の最適化など、フロントエンドを最適化します。 6)パフォーマンスを継続的に監視および最適化します。これらの方法により、PHPアプリケーションのパフォーマンスを大幅に改善できます。

依存関係(di)inphpisadesignpatternativats anducesclassodulencies、拡張測定性、テスト可能性、および維持可能性。

cachingemprovesppperformancebystring of computationsorquickretrieval、還元装置の削減は、reducingerloadendenhancersponseTimes.efcectivestrategiesInclude:1)opcodecaching、compiledphpscriptsinmemorytoskipcompilation;


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター
