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.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流