简体   繁体   English

如何检查 SplFixedArray 是否包含值?

[英]how to check if SplFixedArray contains value?

with a normal array one would use in_array() , but in_array doesn't support SplFixedArray, and i couldn't really find any splFixedArray in_array() equivalent, i could just foreach-loop it myself like使用普通数组可以使用in_array() ,但是 in_array 不支持 SplFixedArray,而且我真的找不到任何 splFixedArray in_array() 等价物,我可以自己进行 foreach 循环

function spl_in_array(\SplFixedArray &$arr, $value): bool
{
    foreach ($arr as $val) {
        if ($val === $value) {
            return true;
        }
    }
    return false;
}

but that appears to be ~14-17 times slower than in_array(), and since the whole point of SplFixedArray is performance and memory usage (i think?)... with this benchmark code但这似乎比 in_array() 慢了 ~14-17 倍,并且由于 SplFixedArray 的全部意义在于性能和 memory 的使用(我认为?)......使用这个基准代码

<?php
declare(strict_types = 1);

$check_iterations = 100;
$nbr_array_values = 100000;
$lookup_value = round($nbr_array_values / 2);

function format(float $f): string
{
    return number_format($f, 6);
}

function spl_in_array(\SplFixedArray &$arr, $value): bool
{
    foreach ($arr as $val) {
        if ($val === $value) {
            return true;
        }
    }
    return false;
}

$arr = [];
for ($i = 0; $i < $nbr_array_values; ++ $i) {
    $arr[] = $i;
}
$splArr = SplFixedArray::fromArray($arr);
$results = [];
$start = microtime(true);
for ($i = 0; $i < $check_iterations; ++ $i) {
    in_array($lookup_value, $arr, true);
}
$end = microtime(true);
$results["in_array"] = $end - $start;
$start = microtime(true);
for ($i = 0; $i < $check_iterations; ++ $i) {
    spl_in_array($splArr, $lookup_value);
}
$end = microtime(true);
$results["spl_in_array"] = ($end - $start);

$results["in_array/spl_in_array"] = $results["in_array"] / $results["spl_in_array"];
$results["spl_in_array/in_array"] = $results["spl_in_array"] / $results["in_array"];
$fastest = ($results["in_array"] > $results["spl_in_array"]) ? "spl_in_array" : "in_array";
foreach ($results as &$result) {
    $result = format($result);
}
$results["fastest"] = $fastest;
print_r($results);

i get我明白了

Array
(
    [in_array] => 0.001330
    [spl_in_array] => 0.019912
    [in_array/spl_in_array] => 0.066801
    [spl_in_array/in_array] => 14.969887
    [fastest] => in_array
)
Array
(
    [in_array] => 0.001024
    [spl_in_array] => 0.015516
    [in_array/spl_in_array] => 0.065997
    [spl_in_array/in_array] => 15.152270
    [fastest] => in_array
)
Array
(
    [in_array] => 0.001124
    [spl_in_array] => 0.015159
    [in_array/spl_in_array] => 0.074140
    [spl_in_array/in_array] => 13.487908
    [fastest] => in_array
)
Array
(
    [in_array] => 0.000838
    [spl_in_array] => 0.013853
    [in_array/spl_in_array] => 0.060495
    [spl_in_array/in_array] => 16.530299
    [fastest] => in_array
)
Array
(
    [in_array] => 0.000960
    [spl_in_array] => 0.014201
    [in_array/spl_in_array] => 0.067609
    [spl_in_array/in_array] => 14.790911
    [fastest] => in_array
)
Array
(
    [in_array] => 0.000865
    [spl_in_array] => 0.015183
    [in_array/spl_in_array] => 0.056970
    [spl_in_array/in_array] => 17.553197
    [fastest] => in_array
)

so doing it manually with a foreach() is evidently not a particularly efficient way of doing it..因此使用 foreach() 手动执行显然不是一种特别有效的方法。

hence the question, how should i check if a SplFixedArray contains something?因此问题是,我应该如何检查 SplFixedArray 是否包含某些内容? what is the SplFixedArray-equivalent of in_array() ? in_array()的 SplFixedArray 等效项是什么?

If you make use of the ->toArray() method to convert the spl array to a standard PHP Array and then use in_array() you can reduce the time enormously如果使用->toArray()方法将 spl 数组转换为标准 PHP 数组,然后使用in_array()可以大大减少时间

$check_iterations = 100;
$nbr_array_values = 100000;
$lookup_value = round($nbr_array_values / 2);

function format(float $f): string
{
    return number_format($f, 6);
}

function spl_in_array(\SplFixedArray &$arr, $value): bool
{
    foreach ($arr as $val) {
        if ($val === $value) {
            return true;
        }
    }
    return false;
}

function new_spl_in_array(\SplFixedArray &$arr, $value): bool
{
    $a = $arr->toArray();
    return in_array($value, $a);
}

$arr = [];
for ($i = 0; $i < $nbr_array_values; ++ $i) {
    $arr[] = $i;
}
$splArr = SplFixedArray::fromArray($arr);
$results = [];
$start = microtime(true);
for ($i = 0; $i < $check_iterations; ++ $i) {
    in_array($lookup_value, $arr, true);
}
$end = microtime(true);
$results["in_array"] = $end - $start;
$start = microtime(true);
for ($i = 0; $i < $check_iterations; ++ $i) {
    spl_in_array($splArr, $lookup_value);
}
$end = microtime(true);
$results["spl_in_array"] = ($end - $start);

$start = microtime(true);
for ($i = 0; $i < $check_iterations; ++ $i) {
    new_spl_in_array($splArr, $lookup_value);
}
$end = microtime(true);
$results["spl_in_array_new"] = ($end - $start);

$results["in_array/spl_in_array"] = $results["in_array"] / $results["spl_in_array"];
$results["spl_in_array/in_array"] = $results["spl_in_array"] / $results["in_array"];
$fastest = ($results["in_array"] > $results["spl_in_array"]) ? "spl_in_array" : "in_array";
foreach ($results as &$result) {
    $result = format($result);
}
$results["fastest"] = $fastest;
print_r($results);

RESULTS结果

Array
(
    [in_array] => 0.011569
    [spl_in_array] => 1.094222
    [spl_in_array_new] => 0.157041      // < Using ->toArray()
    [in_array/spl_in_array] => 0.010573
    [spl_in_array/in_array] => 94.583991
    [fastest] => in_array
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM