PHP:in_array() 对比 array_intersect() 性能
PHP: in_array() vs array_intersect() performance
什么和多少更快 - 使用 foreach
手动遍历数组并使用 in_array()
检查是否出现 needle
,或使用 array_intersect()
?
基准
测试脚本
<?php
$numbers = range(32, 127);
$numbersLetters = array_map('chr', $numbers);
for (;;) {
$numbersLetters = array_merge($numbersLetters, $numbersLetters);
if (count($numbersLetters) > 10000) {
break;
}
}
$numbers = range(1, count($numbersLetters));
printf("Sample size: %d elements in 2 arrays (%d total) \n", count($numbers), count($numbers) + count($numbersLetters));
printf("Benchmarking speed in foreach + in_array() scenario... (this might take a while) ");
shuffle($numbers);
shuffle($numbersLetters);
$t1 = microtime(true);
foreach ($numbers as $number) {
if (in_array($number, $numbersLetters)) {}
}
$t2 = microtime(true);
printf("DONE!\n");
printf("Time elapsed: %.5f \n", $t2 - $t1);
// =============================------------===============================
printf("Benchmarking speed with array_intersect...");
shuffle($numbers);
shuffle($numbersLetters);
$t1 = microtime(true);
array_intersect($numbers, $numbersLetters);
$t2 = microtime(true);
printf("DONE!\n");
printf("Time elapsed: %.5f \n", $t2 - $t1);
输出
Sample size: 12288 elements in 2 arrays (24576 total)
Benchmarking speed in foreach + in_array() scenario... (this might take a while) DONE!
Time elapsed: 3.79213
Benchmarking speed with array_intersect...DONE!
Time elapsed: 0.05765
Fiddle: http://ideone.com/OZ2Idf
结论
array_intersect
比 foreach
+ in_array
.
快得多
为什么 array_intersect
更快?
- 交集是用 C 代码计算的,而不是 PHP
- 在内部,Zend 引擎 sorts the array prior to computing the intersection (why is processing a sorted array faster?)
什么和多少更快 - 使用 foreach
手动遍历数组并使用 in_array()
检查是否出现 needle
,或使用 array_intersect()
?
基准
测试脚本
<?php
$numbers = range(32, 127);
$numbersLetters = array_map('chr', $numbers);
for (;;) {
$numbersLetters = array_merge($numbersLetters, $numbersLetters);
if (count($numbersLetters) > 10000) {
break;
}
}
$numbers = range(1, count($numbersLetters));
printf("Sample size: %d elements in 2 arrays (%d total) \n", count($numbers), count($numbers) + count($numbersLetters));
printf("Benchmarking speed in foreach + in_array() scenario... (this might take a while) ");
shuffle($numbers);
shuffle($numbersLetters);
$t1 = microtime(true);
foreach ($numbers as $number) {
if (in_array($number, $numbersLetters)) {}
}
$t2 = microtime(true);
printf("DONE!\n");
printf("Time elapsed: %.5f \n", $t2 - $t1);
// =============================------------===============================
printf("Benchmarking speed with array_intersect...");
shuffle($numbers);
shuffle($numbersLetters);
$t1 = microtime(true);
array_intersect($numbers, $numbersLetters);
$t2 = microtime(true);
printf("DONE!\n");
printf("Time elapsed: %.5f \n", $t2 - $t1);
输出
Sample size: 12288 elements in 2 arrays (24576 total)
Benchmarking speed in foreach + in_array() scenario... (this might take a while) DONE!
Time elapsed: 3.79213
Benchmarking speed with array_intersect...DONE!
Time elapsed: 0.05765
Fiddle: http://ideone.com/OZ2Idf
结论
array_intersect
比 foreach
+ in_array
.
为什么 array_intersect
更快?
- 交集是用 C 代码计算的,而不是 PHP
- 在内部,Zend 引擎 sorts the array prior to computing the intersection (why is processing a sorted array faster?)