468,278 Members | 1,580 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,278 developers. It's quick & easy.

file_get_contents VS fread

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.

Nov 7 '06 #1
5 22209
Hi,
are there any advantage in replacing all fread() operations with
file_get_contents() ?
You need the handle provided by fopen() for many operations
like fgetcsv().

Another advantage of fread() is that you can read just a said
amount of bytes from a line - you don't have to read the full
file at once.

But if you just need the full contents, file_get_contents()
is an easier option.

--
Binny V A
http://www.bin-co.com/

Nov 7 '06 #2
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.
js*******@attglobal.net
==================
Nov 7 '06 #3

Jerry Stuckle gDG
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.
js*******@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?

Nov 7 '06 #4
howa wrote:
Jerry Stuckle gDG

>>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.
js*******@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?
If you need to read them all at once, then fine. There won't be a lot
of memory difference between reading everything in one
file_get_contents() and multiple fread()s.

The question is - do you? Are you operating on the entire file at one
time? Most cases unless I'm actually writing a file to the browser I
find I'm only working on a small piece at a time

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 7 '06 #5
On 7 Nov 2006 00:28:06 -0800, "howa" <ho******@gmail.comwrote:
>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?
The main difference between a complete file read using fread and
file_get_contents is that file_get_contents may use mmap (if your OS supports
it) to use file memory mapping, which can eliminate a little of the copying
and/or allocation of memory in getting it from the file to PHP's memory space

Whether it makes any difference to your system depends on your system and you
should benchmark it in relation to the rest of your script before worrying too
much about it.

Always beware of premature optimisation - you probably have larger things to
worry about first - file_get_contents() may be a slightly faster way to read
the whole file into memory, but it's possible that it may be more efficient to
read the file in chunks and process those smaller chunks - everything depends
on context.

For reference, here are some timings from a trivial pair of scripts against a
70kB file:

Rate fread file_get_contents
fread 1562/s -- -9%
file_get_contents 1724/s 10% --

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Nov 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ken | last post: by
2 posts views Thread by ggg | last post: by
5 posts views Thread by vito | last post: by
13 posts views Thread by 010 010 | last post: by
5 posts views Thread by David Mathog | last post: by
4 posts views Thread by Aetherweb | last post: by
20 posts views Thread by ericunfuk | last post: by
reply views Thread by pac1250 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.