dscpg80066 2008-10-28 00:52
浏览 111

什么是PHP的fgets()使它在大文件上如此可怕?

What is it about fgets() implementation that makes it so horrible on large files vs fread?

To demonstrate, run this code:

<?php
$line = str_repeat('HelloWorld', 100000) . "
";
for($i=0; $i<10000; ++$i)
    file_put_contents('myfile', $line, FILE_APPEND);
//now we have roughly a 1gig file

// We'll even let fread go first incase
// the subsequent test would have any caching benefits
$fp = fopen('myfile2','r');
$start = microtime(true);
while (fread($fp,4096));
$end = microtime(true);
print ($end-$start) . " using fread
";
fseek($fp, 0);

$start = microtime(true);
while (fgets($fp));
$end = microtime(true);
print ($end-$start) . " using fgets
";
?>
  • 写回答

1条回答 默认 最新

  • douzhenao6515 2008-10-28 02:36
    关注

    It might have something to do with how you're calling fgets. From the manual, it says that if you leave the second parameter out, then:

    If no length is specified, it will keep reading from the stream until it reaches the end of the line.

    If the data your working with has some very long lines (eg: "HelloWorld" 100,000 times = 1,000,000 characters), then it has to read that whole thing.

    Again, from the manual:

    If the majority of the lines in the file are all larger than 8KB, it is more resource efficient for your script to specify the maximum line length.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?