The difference in speed between fread and file_get_contents

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

The difference in speed between fread and file_get_contents

Post by socket1 »

I haven't really put much thought to whether writing file_get_contents('./path/to/local/file.txt') was faster than doing the whole fopen('file', 'r') thing but I thought it'd be time to test this.

I wrote a simple script that read a 20KB txt file and timed it using file_get_contents and fread:

http://ajarsource.org/projects/file_rea ... tspeed.php

it looks something like this:

FREAD Time:
0.00012397766113281
file_get_contents Time:
0.00010180473327637

It generally shows that file_get_contents is faster than fread and all of its accompanying functions.

I am just wondering what the pros/cons of using either method are and should I be using one over the other for local file reading?


Source: http://ajarsource.org/projects/file_reading/src.php
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: The difference in speed between fread and file_get_contents

Post by Weirdan »

socket1 wrote:It generally shows that file_get_contents is faster than fread and all of its accompanying functions.
I'd say it generally shows the difference is in order of hundreds of microseconds (read: negligible). fopen() + fread() + fseek() gives you more control of what exactly you're reading and you can dispose data that you processed and don't need any longer easily, while file_get_contents() maps the file into the memory and it gets loaded as you access resulting string, making freeing much harder (I guess in this case it would require reallocating shorter string and copying data there).
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: The difference in speed between fread and file_get_contents

Post by kaisellgren »

I would say do what ever you need to do. If you need the whole file, just use file_get_contents(), if you need certain parts of it, use fread()/fopen() and neighbors.
Tobey
Forum Newbie
Posts: 12
Joined: Thu May 14, 2009 11:40 am
Location: Germany

Re: The difference in speed between fread and file_get_contents

Post by Tobey »

fopen/fread/fseek: more control, slower, less memory
file_get_contents: faster, no control, a lot of memory
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: The difference in speed between fread and file_get_contents

Post by kaisellgren »

Tobey wrote:fopen/fread/fseek: more control, slower, less memory
file_get_contents: faster, no control, a lot of memory
I don't know how did you get those, but for me file_get_contents() consumes a lot less memory and is 50% faster than the fopen() approach. If you need to deal with large files, then fopen() will beat file_get_contents() hands down.
Tobey
Forum Newbie
Posts: 12
Joined: Thu May 14, 2009 11:40 am
Location: Germany

Re: The difference in speed between fread and file_get_contents

Post by Tobey »

That's what I meant. file_get_contents() reads the whole content into memory and cannot be used for large files. Depending on the size of your buffer, fread() will always use the same amount of memory.
If you want to load many small files, file_get_contents() will be better, but if you have to handle thousands of requests at the same time and have to load a 100mb file everytime, fread() will be the only solution.
Bruno De Barros
Forum Commoner
Posts: 82
Joined: Mon May 12, 2008 8:41 am
Location: Ireland

Re: The difference in speed between fread and file_get_contents

Post by Bruno De Barros »

FREAD Time:
0.012369155883789
file_get_contents Time:
0.00011801719665527
Yet another proof that it's always better to repeat the same test over and over again, to render these one-of-a-kind results null. :lol:
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: The difference in speed between fread and file_get_contents

Post by socket1 »

I'm not sure if you're supposed to but I use file_get_contents('http://google.com'); for different sites occasionally... and I don't think fopen + fread can do that.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: The difference in speed between fread and file_get_contents

Post by kaisellgren »

Tobey wrote:Depending on the size of your buffer,
The buffer should be as big as the file, otherwise a direct comparison isn't really fair.. we can only compare different situations which seems kinda foolish imo.
socket1 wrote:I'm not sure if you're supposed to but I use file_get_contents('http://google.com'); for different sites occasionally... and I don't think fopen + fread can do that.
fopen() does support wrappers.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: The difference in speed between fread and file_get_contents

Post by socket1 »

Ah, allow_url_fopen has to be enabled right?
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: The difference in speed between fread and file_get_contents

Post by kaisellgren »

Tobey
Forum Newbie
Posts: 12
Joined: Thu May 14, 2009 11:40 am
Location: Germany

Re: The difference in speed between fread and file_get_contents

Post by Tobey »

kaisellgren wrote:
Tobey wrote:Depending on the size of your buffer,
The buffer should be as big as the file, otherwise a direct comparison isn't really fair.. we can only compare different situations which seems kinda foolish imo.
Ehm, it does not make sense to compare the same situations. Why would you use fopen() if you want to read the whole content of a file into memory at once? file_get_contents() will beat fopen() everytime, because there is no overhead, unless the interpreter optimizes the fopen() call so that it treats fopen(), fread() and fclose() like file_get_contents(). In my opinion fopen() is not intended to be used in this way!
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: The difference in speed between fread and file_get_contents

Post by kaisellgren »

Tobey wrote:Why would you use fopen() if you want to read the whole content of a file into memory at once?
I'm not talking about using, I'm talking about comparing. :P

I am (/was) talking about which one performs better when loading a whole file, because otherwise it does not make sense to compare as we would use the function which is more convenient to the case. Anyway, points taken.
Post Reply