|
Posted by howa on 11/07/06 12:58
Jerry Stuckle 寫道:
> howa wrote:
> > are there any advantage in replacing all fread() operations with
> > file_get_contents() ?
> >
> > i.e.
> >
> > file_get_contents("/usr/local/something.txt")
> >
> > VS
> >
> > $filename = "/usr/local/something.txt";
> > $handle = fopen($filename, "r");
> > $contents = fread($handle, filesize($filename));
> > fclose($handle);
> >
> > is that file_get_contents() is more efficient?
> >
> > thanks.
> >
>
> Like almost anything else in programming, "it depends".
>
> file_get_contents() can be faster because it's a single call to read
> the file. But it can also be slower - because it reads the entire
> file into memory at one time.
>
> If you're reading 5MB files, file_get_contents() will take something
> more than 5MB of RAM. Stack a few of these up and you'll be using
> a LOT of RAM - maybe too much and the system can start paging.
>
> fread(), OTOH, only gets small amounts of data at a time. And yes,
> for large files it can take longer than file_get_contents(), but for
> small files I don't think you'll notice any difference.
>
> If you're not having performance problems, I'd say don't worry about it.
>
> If you are having performance problems, I suggest you figure out where
> the real bottleneck is. I doubt it's in the use of fread().
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstucklex@attglobal.net
> ==================
well, if the file size is usually less than 100K, and i need to read
them all once, so file_get_contents() is preferred?
Navigation:
[Reply to this message]
|