在 PHP 中计算数组中巧合的最佳方法
Best way to count coincidences in an Array in PHP
我有一个包含数千行的数组,我想知道计算 PHP 中有巧合的行数的最佳方法或最佳做法是什么。
在示例中,您可以看到我可以找到与某个范围匹配的记录数。
我在考虑这 2 个选项:
选项 1:
$rowCount = 0;
foreach ($this->data as $row) {
if ($row['score'] >= $rangeStart && $row['score'] <= $rangeEnd) {
$rowCount++;
}
}
return $rowCount;
选项 2:
$countScoreRange = array_filter($this->data, function($result) {
return $result['score'] >= $this->rangeStart && $result['score'] <= $this->rangeEnd;
});
return count($countScoreRange);
提前致谢。
使用 foreach 快得多,增加计数也比使用 count()
更快。
我曾经测试过这两种性能,foreach 比 array_filter 快大约 3 倍。
我会选择第一个选项。
这取决于您在谈论最佳实践时的意思?
如果您关于最佳实践的想法是关于性能的,那么可以说有一个权衡是您必须关心的
**speed <===> memory**
如果您需要有关内存的性能:
以这种方式:当您考虑在 PHP 中迭代可迭代对象的性能时,您可以使用 YIELD
创建一个 生成器函数 , 从 PHP doc :
什么是生成器函数?
A generator function looks just like a normal function, except that instead of returning a value, a generator yields as many values as it needs to. Any function containing yield is a generator function.
为什么我们必须使用生成器函数?
A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate.
所以最好分配一点内存而不是保留数千个数组
Unfortunately, PHP also does not allow you to use the array traversal functions on generators, including array_filter, array_map, etc.
所以到这里你知道是否:
- 你正在迭代一个数组
- 千元
- 特别是如果你在函数中使用它并且函数运行很多地方。
- 并且您特别关心内存使用方面的性能。
强烈建议改用生成器函数。
** 但如果您需要关于速度的性能:**
比较大约有 4 件事 :
for
foreach
array_*
函数
- 数组函数(就像
nexT()
、 reset()
等)
'foreach' 与 'for' 循环相比速度较慢。 foreach 复制需要执行迭代的数组。
但如果您想使用它,您可以 some tricks :
为了提高性能,需要使用引用的概念。除此之外,‘foreach’也很容易使用。
foreach
和 array-filter
呢?
正如之前的回答所说,也是基于此 article , also this : :
it is incorrect when we thought that array_* functions are faster!
Of course, if you work on the critical system you should consider this advice. Array functions are a little bit slower than basic loops but I think that there is no significant impact on performance.
我有一个包含数千行的数组,我想知道计算 PHP 中有巧合的行数的最佳方法或最佳做法是什么。
在示例中,您可以看到我可以找到与某个范围匹配的记录数。
我在考虑这 2 个选项:
选项 1:
$rowCount = 0;
foreach ($this->data as $row) {
if ($row['score'] >= $rangeStart && $row['score'] <= $rangeEnd) {
$rowCount++;
}
}
return $rowCount;
选项 2:
$countScoreRange = array_filter($this->data, function($result) {
return $result['score'] >= $this->rangeStart && $result['score'] <= $this->rangeEnd;
});
return count($countScoreRange);
提前致谢。
使用 foreach 快得多,增加计数也比使用 count()
更快。
我曾经测试过这两种性能,foreach 比 array_filter 快大约 3 倍。
我会选择第一个选项。
这取决于您在谈论最佳实践时的意思?
如果您关于最佳实践的想法是关于性能的,那么可以说有一个权衡是您必须关心的
**speed <===> memory**
如果您需要有关内存的性能:
以这种方式:当您考虑在 PHP 中迭代可迭代对象的性能时,您可以使用 YIELD
创建一个 生成器函数 , 从 PHP doc :
什么是生成器函数?
A generator function looks just like a normal function, except that instead of returning a value, a generator yields as many values as it needs to. Any function containing yield is a generator function.
为什么我们必须使用生成器函数?
A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate.
所以最好分配一点内存而不是保留数千个数组
Unfortunately, PHP also does not allow you to use the array traversal functions on generators, including array_filter, array_map, etc.
所以到这里你知道是否:
- 你正在迭代一个数组
- 千元
- 特别是如果你在函数中使用它并且函数运行很多地方。
- 并且您特别关心内存使用方面的性能。
强烈建议改用生成器函数。
** 但如果您需要关于速度的性能:**
比较大约有 4 件事 :
for
foreach
array_*
函数- 数组函数(就像
nexT()
、reset()
等)
'foreach' 与 'for' 循环相比速度较慢。 foreach 复制需要执行迭代的数组。
但如果您想使用它,您可以 some tricks :
为了提高性能,需要使用引用的概念。除此之外,‘foreach’也很容易使用。
foreach
和 array-filter
呢?
正如之前的回答所说,也是基于此 article , also this : :
it is incorrect when we thought that array_* functions are faster!
Of course, if you work on the critical system you should consider this advice. Array functions are a little bit slower than basic loops but I think that there is no significant impact on performance.