472,805 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 22868
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 ¼g¹D¡G
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 ¼g¹D¡G

>>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rob | last post by:
for some reason i keep getting : Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /home/rob/www/php/filegetcontents2.php on...
2
by: Ken | last post by:
I am trying to read a JavaScript file. Permissions have been set to 755. Do I have to open the file first? fopen($filename, "rb"); Then read it? My script:
2
by: ggg | last post by:
Here's what I'm trying to do. The server serves up XML documents based on what I specify in the GET string. This is on a server I have no control over. There is a lag between the time when I...
5
by: vito | last post by:
a week ago 2 kind persons suggested me to use fread to read a result page. i've been working for it but just can't get what i want. $keyword ="big problem"; $startfig = rand(0,60); //$theFile...
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
4
by: Aetherweb | last post by:
This is probably really obvious, sorry, been a long day... I'm wanting to create a PHP file which is a template for an email, and read the file into a string, ready to send out using my email...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
0
by: pac1250 | last post by:
Hi, I am searching how to solve a problem and I dont find it :( I want to access a page from a script behind a proxy : (my script) <-(a proxy with authentification) <-(https serveur with...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.