使用 PHP 处理十亿行数据,如何极致提升处理速度?( 三 )

< $station[0]) {$station[0] = $temp;} elseif ($temp > $station[1]) {$station[1] = $temp;}} else {$stations[$city] = [$temp,$temp,$temp,1];}}return $stations;};$chunks = get_file_chunks($file, $threads_cnt);$futures = [];for ($i = 0; $i < $threads_cnt; $i++) {$runtime = new parallelRuntime();$futures[$i] = $runtime->run($process_chunk,[$file,$chunks[$i][0],$chunks[$i][1]]);}$results = [];for ($i = 0; $i < $threads_cnt; $i++) {// 等待线程结果,主线程在此处阻塞直至获取结果$chunk_result = $futures[$i]->value();foreach ($chunk_result as $city => $measurement) {if (isset($results[$city])) {$result = &$results[$city];$result[2] += $measurement[2];$result[3] += $measurement[3];if ($measurement[0] < $result[0]) {$result[0] = $measurement[0];}if ($measurement[1] > $result[1]) {$result[1] = $measurement[1];}} else {$results[$city] = $measurement;}}}ksort($results);echo '{', PHP_EOL;foreach ($results as $k => &$station) {echo "t", $k, '=', $station[0], '/', ($station[2] / $station[3]), '/', $station[1], ',', PHP_EOL;}echo '}', PHP_EOL;该段代码主要执行以下操作:首先,它扫描文件并将其分割成以 n 为界的块(利用 fgets() 进行读?。?。准备好这些块后,我启动了 $threads_cnt 个工作线程 , 它们分别打开相同的文件并跳转到分配给它们的块的起始位置,继续读取并处理数据直到块结束 , 返回中间结果 。最后,在主线程中合并、排序并输出这些结果 。
利用多线程处理,这个过程只需:



推荐阅读