Reply to Re: file_get_contents VS fread

Your name:

Reply:


Posted by Andy Hassall on 11/07/06 17:08

On 7 Nov 2006 00:28:06 -0800, "howa" <howachen@gmail.com> 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?

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 :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация