473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 22978
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.