The difference in speed between fread and file_get_contents
Moderator: General Moderators
The difference in speed between fread and file_get_contents
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
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
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).socket1 wrote:It generally shows that file_get_contents is faster than fread and all of its accompanying functions.
- 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
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
fopen/fread/fseek: more control, slower, less memory
file_get_contents: faster, no control, a lot of memory
file_get_contents: faster, no control, a lot of memory
- 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
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 wrote: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
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.
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
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.FREAD Time:
0.012369155883789
file_get_contents Time:
0.00011801719665527
Re: The difference in speed between fread and file_get_contents
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.
- 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
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.Tobey wrote:Depending on the size of your buffer,
fopen() does support wrappers.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.
Re: The difference in speed between fread and file_get_contents
Ah, allow_url_fopen has to be enabled right?
- 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
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!kaisellgren wrote: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.Tobey wrote:Depending on the size of your buffer,
- 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
I'm not talking about using, I'm talking about comparing.Tobey wrote:Why would you use fopen() if you want to read the whole content of a file into memory at once?
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.