我正在使用 PHP CLI 提供标准输入。我是否使用了读取该输入的最佳方法?
比如我给它提供50000行数据。每行包含两个数字。我下面的代码是读取 50,000 行数据的最有效方式吗?或者这是一种非常低效的方法?
这是我的代码:
<?php
// Testing time period for execution
// Time tracker: TESTING
$micropoint1 = microtime(true);
// First, retrieve the number of points that will be provided.
$no_points = fgets(STDIN);
for($i=1, $max=$no_points+1; $i<$max; $i++) {
list($x, $y) = fscanf(STDIN, "%d %d"); // Get the string returned from the command line and convert to an array
}
// Time tracker: TESTING
$micropoint2 = microtime(true);
$pointelapsed = $micropoint2 - $micropoint1;
fwrite(STDOUT, "\nPoint Loop Took ".$pointelapsed." microsecs\n");
?>
最佳答案
我无法想象您的方法会变得更加高效。
关于php - 我的代码是否使用最佳方式来读取 php 中的标准输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7373430/