对于普通的数组,我们可以使用in_array()
,但in_array不支持SplFixedArray,而且我也找不到任何与splFixedArray in_array()等效的函数,我可以像这样自己执行循环
function spl_in_array(\SplFixedArray &$arr, $value): bool
{
foreach ($arr as $val) {
if ($val === $value) {
return true;
}
}
return false;
}
但这似乎比in_array()慢了大约14-17倍,而且由于SplFixedArray的重点是性能和内存使用(我认为?)...使用此基准测试代码
<?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);
我得到了
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
)
因此,使用foreach()手动执行此操作显然不是一种特别有效的方法。
因此出现了这样一个问题:我应该如何检查SplFixedArray是否包含某些内容?in_array()
的SplFixedArray等价物是什么?
发布于 2020-05-16 20:53:21
如果使用->toArray()
方法将spl数组转换为标准PHP array,然后使用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);
结果
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
)
https://stackoverflow.com/questions/61842807
复制