Page 1 of 1

The difference in speed between fread and file_get_contents

Posted: Sat Jun 20, 2009 8:37 pm
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

Re: The difference in speed between fread and file_get_contents

Posted: Sat Jun 20, 2009 8:59 pm
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).

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 4:52 am
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.

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 5:26 am
by Tobey
fopen/fread/fseek: more control, slower, less memory
file_get_contents: faster, no control, a lot of memory

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 5:54 am
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.

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 6:17 am
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.

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 6:41 am
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:

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 8:13 am
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.

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 9:27 am
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.

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 9:35 am
by socket1
Ah, allow_url_fopen has to be enabled right?

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 9:43 am
by kaisellgren

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 2:58 pm
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!

Re: The difference in speed between fread and file_get_contents

Posted: Sun Jun 21, 2009 3:26 pm
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.