array_unique与array_flip [英] array_unique vs array_flip
问题描述
如果我有一个有符号整数数组,例如:
Array
(
[0] => -3
[1] => 1
[2] => 2
[3] => 3
[4] => 3
)
要获得唯一值,我会本能地使用array_unique
,但经过考虑,我可以执行两次array_flip
,效果相同,而且我认为这样会更快?
array_unique
O(n log n),因为它使用了排序操作
array_flip
O(n)
我的假设是否正确?
更新/示例:
$intArray1 = array(-4,1,2,3);
print_r($intArray1);
$intArray1 = array_flip($intArray1);
print_r($intArray1);
$intArray1 = array_flip($intArray1);
print_r($intArray1);
Array
(
[0] => -3
[1] => 1
[2] => 2
[3] => 3
[4] => 3
)
Array
(
[-3] => 0
[1] => 1
[2] => 2
[3] => 4
)
Array
(
[0] => -3
[1] => 1
[2] => 2
[4] => 3
)
我为您进行了基准测试: CodePad
您对此的直觉是正确的!
$test=array();
for($run=0; $run<1000; $run++)
$test[]=rand(0,100);
$time=microtime(true);
for($run=0; $run<100; $run++)
$out=array_unique($test);
$time=microtime(true)-$time;
echo 'Array Unique: '.$time."\n";
$time=microtime(true);
for($run=0; $run<100; $run++)
$out=array_keys(array_flip($test));
$time=microtime(true)-$time;
echo 'Keys Flip: '.$time."\n";
$time=microtime(true);
for($run=0; $run<100; $run++)
$out=array_flip(array_flip($test));
$time=microtime(true)-$time;
echo 'Flip Flip: '.$time."\n";
输出:
Array Unique: 1.1829199790955
Keys Flip: 0.0084578990936279
Flip Flip: 0.0083951950073242
请注意,array_keys(array_flip($array))
将按顺序给出新的键值,这在许多情况下可能是您想要的(相同的,除了要快到array_values(array_unique($array))
以外),而array_flip(array_flip($array))
是相同的(除了要快得多)到array_unique($array)
按键保持不变.
If I had an array of signed integers e.g:
Array
(
[0] => -3
[1] => 1
[2] => 2
[3] => 3
[4] => 3
)
To get unique values I would instinctively use array_unique
but after consideration I could perform array_flip
twice which would have the same effect, and I think it would be quicker?
array_unique
O(n log n) because of the sort operation it uses
array_flip
O(n)
Am I correct in my assumptions?
UPDATE / EXAMPLE:
$intArray1 = array(-4,1,2,3);
print_r($intArray1);
$intArray1 = array_flip($intArray1);
print_r($intArray1);
$intArray1 = array_flip($intArray1);
print_r($intArray1);
Array
(
[0] => -3
[1] => 1
[2] => 2
[3] => 3
[4] => 3
)
Array
(
[-3] => 0
[1] => 1
[2] => 2
[3] => 4
)
Array
(
[0] => -3
[1] => 1
[2] => 2
[4] => 3
)
I benchmarked it for you: CodePad
Your intuition on this was correct!
$test=array();
for($run=0; $run<1000; $run++)
$test[]=rand(0,100);
$time=microtime(true);
for($run=0; $run<100; $run++)
$out=array_unique($test);
$time=microtime(true)-$time;
echo 'Array Unique: '.$time."\n";
$time=microtime(true);
for($run=0; $run<100; $run++)
$out=array_keys(array_flip($test));
$time=microtime(true)-$time;
echo 'Keys Flip: '.$time."\n";
$time=microtime(true);
for($run=0; $run<100; $run++)
$out=array_flip(array_flip($test));
$time=microtime(true)-$time;
echo 'Flip Flip: '.$time."\n";
Output:
Array Unique: 1.1829199790955
Keys Flip: 0.0084578990936279
Flip Flip: 0.0083951950073242
Note that array_keys(array_flip($array))
will give a new key values in order, which in many cases may be what you want (identical except much faster to array_values(array_unique($array))
), whereas array_flip(array_flip($array))
is identical (except much faster) to array_unique($array)
where the keys remain the same.
这篇关于array_unique与array_flip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!