【发布时间】:2017-07-30 13:38:33
【问题描述】:
我有一个长期运行的 PHP 守护程序,它有一个扩展 ArrayIterator 的集合类。这包含一组自定义 Column 对象,通常少于 1000 个。通过 xdebug 分析器运行它,我发现我的 find 方法消耗了大约 35% 的周期。
如何以优化的方式在内部迭代项目?
class ColumnCollection extends \ArrayIterator
{
public function find($name)
{
$return = null;
$name = trim(strtolower($name));
$this->rewind();
while ($this->valid()) {
/** @var Column $column */
$column = $this->current();
if (strtolower($column->name) === $name) {
$return = $column;
break;
}
$this->next();
}
$this->rewind();
return $return;
}
}
【问题讨论】:
标签: php optimization arrayiterator