473,882 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file_get_conten ts VS fread

are there any advantage in replacing all fread() operations with
file_get_conten ts() ?

i.e.

file_get_conten ts("/usr/local/something.txt")

VS

$filename = "/usr/local/something.txt";
$handle = fopen($filename , "r");
$contents = fread($handle, filesize($filen ame));
fclose($handle) ;

is that file_get_conten ts() is more efficient?

thanks.

Nov 7 '06 #1
5 23044
Hi,
are there any advantage in replacing all fread() operations with
file_get_conten ts() ?
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_conten ts()
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_conten ts() ?

i.e.

file_get_conten ts("/usr/local/something.txt")

VS

$filename = "/usr/local/something.txt";
$handle = fopen($filename , "r");
$contents = fread($handle, filesize($filen ame));
fclose($handle) ;

is that file_get_conten ts() is more efficient?

thanks.
Like almost anything else in programming, "it depends".

file_get_conten ts() 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_conten ts() 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_conten ts(), 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*******@attgl obal.net
=============== ===
Nov 7 '06 #3

Jerry Stuckle ¼g¹D¡G
howa wrote:
are there any advantage in replacing all fread() operations with
file_get_conten ts() ?

i.e.

file_get_conten ts("/usr/local/something.txt")

VS

$filename = "/usr/local/something.txt";
$handle = fopen($filename , "r");
$contents = fread($handle, filesize($filen ame));
fclose($handle) ;

is that file_get_conten ts() is more efficient?

thanks.

Like almost anything else in programming, "it depends".

file_get_conten ts() 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_conten ts() 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_conten ts(), 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*******@attgl obal.net
=============== ===
well, if the file size is usually less than 100K, and i need to read
them all once, so file_get_conten ts() 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_con tents() ?

i.e.

file_get_con tents("/usr/local/something.txt")

VS

$filename = "/usr/local/something.txt";
$handle = fopen($filename , "r");
$contents = fread($handle, filesize($filen ame));
fclose($hand le);

is that file_get_conten ts() is more efficient?

thanks.

Like almost anything else in programming, "it depends".

file_get_cont ents() 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_conten ts() 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_conten ts(), 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*******@att global.net
============= =====


well, if the file size is usually less than 100K, and i need to read
them all once, so file_get_conten ts() 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_conten ts() 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*******@attgl obal.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_conte nts() ?

i.e.

file_get_conte nts("/usr/local/something.txt")

VS

$filename = "/usr/local/something.txt";
$handle = fopen($filename , "r");
$contents = fread($handle, filesize($filen ame));
fclose($handle );

is that file_get_conten ts() is more efficient?
The main difference between a complete file read using fread and
file_get_conten ts is that file_get_conten ts 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_conten ts() 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_conten ts
fread 1562/s -- -9%
file_get_conten ts 1724/s 10% --

--
Andy Hassall :: an**@andyh.co.u k :: 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
4219
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 line 3 Warning: file_get_contents(http://folding.stanford.edu/teamstats/team92.txt): failed to open stream: Resource temporarily unavailable in /home/rob/www/php/filegetcontents2.php on line 3
2
2793
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
2206
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 issue the GET request, and when I actually get the response. (I suspect because the server has to dig for the data and format it. In any case, it's longer than say, a simple HTML page request to the web server.) When I manually cut and paste the...
5
1550
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 ="http://images.google.com/images?q=\"".$keyword."\"&start=".$startfig; //$theFile ="http://images.google.com/images?q=\"".$keyword."\"&start=".strval($startfig);
13
3555
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 discern, fread all the sudden kept returning the same byte over and over as if it were no longer advancing in the file. I used a hex editor to determine the address of the last byte read in the file. CF was the last address, D0 was not ever read...
5
6359
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 time */ I would assume the latter form would be faster, or at least
4
2747
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 functions... but I want to be able to get the PHP file to be server processed so that its content is created on the fly. Eg in pseudo code, what I want to do is this: 1$email_param1 = "my email param 1";
20
7563
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 frequently(forwards and backwards) and do fread() from that offset. Or better still, could anyone let me know some good ways to achieve what I need to do as above?Can I get hold of the file and being able to read in without using fread()? Using...
0
5025
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 authentification) -----------------------------------------------------------------
0
9932
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10726
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9558
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7957
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5782
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5979
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.