php - 我的代码是否使用最佳方式来读取 php 中的标准输入?

标签 php algorithm optimization stdin

我正在使用 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/

相关文章:

c++ - 更改链表反转中的行会导致错误

r - 用于估计负二项分布的 MCMC

c# - mysql外部访问

php - mysql获取每个帖子最频繁值的结果

php - 上传图像(数据为:image/png:base64) to webserver

php - 在六边形场上通过螺旋创建单元格的算法

java - 修剪井字棋 Action

c# - 计算某些范围内的最大有理分数

c++ - 优化递归函数

php - 是否有比 file_get_contents 占用更少内存或更有效的合并 MP3 的方法?