什么更快:in_array 或isset? [英] what is faster: in_array or isset?

查看:15
本文介绍了什么更快:in_array 或isset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题仅针对我,因为我总是喜欢编写可以在廉价的慢速服务器(或流量很大的服务器)上运行的优化代码

This question is merely for me as I always like to write optimized code that can run also on cheap slow servers (or servers with A LOT of traffic)

我环顾四周,找不到答案.我想知道这两个示例之间哪个更快,记住在我的情况下数组的键并不重要(自然是伪代码):

I looked around and I was not able to find an answer. I was wondering what is faster between those two examples keeping in mind that the array's keys in my case are not important (pseudo-code naturally):

<?php
$a = array();
while($new_val = 'get over 100k email addresses already lowercased'){
    if(!in_array($new_val, $a){
        $a[] = $new_val;
        //do other stuff
    }
}
?>

<?php
$a = array();
while($new_val = 'get over 100k email addresses already lowercased'){
    if(!isset($a[$new_val]){
        $a[$new_val] = true;
        //do other stuff
    }
}
?>

由于问题的重点不是数组冲突,我想补充一点,如果您害怕 $a[$new_value] 的插入冲突,您可以使用 $a[md5($new_value)].它仍然会导致冲突,但在读取用户提供的文件时会避免可能的 DoS 攻击 (http://nikic.github.com/2011/12/28/Supercolliding-a-PHP-array.html)

As the point of the question is not the array collision, I would like to add that if you are afraid of colliding inserts for $a[$new_value], you can use $a[md5($new_value)]. it can still cause collisions, but would take away from a possible DoS attack when reading from an user provided file (http://nikic.github.com/2011/12/28/Supercolliding-a-PHP-array.html)

推荐答案

目前的答案是准确的.在这种情况下使用 isset 更快,因为

The answers so far are spot-on. Using isset in this case is faster because

  • 它对键使用 O(1) 哈希搜索,而 in_array 必须检查每个值,直到找到匹配项.
  • 作为操作码,它比调用 in_array 内置函数的开销更少.
  • It uses an O(1) hash search on the key whereas in_array must check every value until it finds a match.
  • Being an opcode, it has less overhead than calling the in_array built-in function.

这些可以通过使用带有值的数组(在下面的测试中为 10,000)来演示,迫使 in_array 进行更多搜索.

These can be demonstrated by using an array with values (10,000 in the test below), forcing in_array to do more searching.

isset:    0.009623
in_array: 1.738441

这是建立在 Jason 的基准测试之上的,填充一些随机值并偶尔找到数组中存在的值.都是随机的,所以要注意时间会波动.

This builds on Jason's benchmark by filling in some random values and occasionally finding a value that exists in the array. All random, so beware that times will fluctuate.

$a = array();
for ($i = 0; $i < 10000; ++$i) {
    $v = rand(1, 1000000);
    $a[$v] = $v;
}
echo "Size: ", count($a), PHP_EOL;

$start = microtime( true );

for ($i = 0; $i < 10000; ++$i) {
    isset($a[rand(1, 1000000)]);
}

$total_time = microtime( true ) - $start;
echo "Total time: ", number_format($total_time, 6), PHP_EOL;

$start = microtime( true );

for ($i = 0; $i < 10000; ++$i) {
    in_array(rand(1, 1000000), $a);
}

$total_time = microtime( true ) - $start;
echo "Total time: ", number_format($total_time, 6), PHP_EOL;

这篇关于什么更快:in_array 或isset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆