1

目前,此脚本大约需要 6 分钟才能完成。


这是我的片段:

$items = getSomeItems();
$results = array();

foreach ($items as $index => $value) {
    $valueA = getValueA($value); // 2 minute process.
    $valueB = getValueB($value); // 2 minute process.
    $valueC = getValueC($value); // 2 minute process.
    $results[] = array('a' => $valueA, 'b' => $valueB, 'c' => $valueC, )
}

return $results;

有什么方法可以并行化这个 foreach 循环吗?我不在乎将数组添加到的$results顺序,无论如何我都会在 SQL 中对它们进行排序。

我可以做类似的事情吗?

parallel-foreach ($items as $index => $value) {
    $valueA = getValueA($value); // 2 minute process.
    $valueB = getValueB($value); // 2 minute process.
    $valueC = getValueC($value); // 2 minute process.
    $results[] = array('a' => $valueA, 'b' => $valueB, 'c' => $valueC, )
}

C# 在Parallel.Foreach中也有类似的想法。我在 PHP 中寻找类似的东西。

4

0 回答 0