Re: read file twice or read into array once?

From: Date: Fri, 05 Oct 2001 05:27:13 +0000
Subject: Re: read file twice or read into array once?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-70032@lists.php.net to get a copy of this message
100 lines of 200 chars each is 20000 which is 20K which is chump change for RAM... Unless you are on a super busy page on a super high-volume server, just file() it. If you're on a super busy page on a super high-volume server, file() it anyway, and then ap benchmark it to see if it's "slim enough" -- And if not, profile it to be sure it's the file() that's killing you, not something else. -- WARNING richard@zend.com address is an endangered species -- Use ceo@l-i-e.com Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm Volunteer a little time: http://chatmusic.com/volunteer.htm ----- Original Message ----- From: John A. Grant <jazrant@zsc.nrcan.zc.ca> Newsgroups: php.general To: <php-general@lists.php.net> Sent: Thursday, October 04, 2001 5:29 PM Subject: read file twice or read into array once? > I have a file containing ~100 lines of text, each perhaps 100-200 > characters in length. I need to retrieve line $n from the file, where: > $n = $julianday % $nlines; > > $julianday = today's date (from localtime[7]) > $nlines = no. of lines in the file > > Method A > 0. fopen() the file > 1. read the file line by line with fgets() and count the lines ($nlines) > 2. compute the required entry: $n=$julianday % $nline > 3. rewind() the file and read it again until entry $n is found > 4. fclose() the file > > Method B > 1. read the entire file into memory: $lines=file("myfile") > 2. compute the required entry: $n=$julianday % count($lines) > > I appreciate that this depends on the no. of lines in the file and > the size of the file. What I'm looking for is opinions on whether > the use of file() is more efficient than fgets(). It needs more memory, > but it eliminates reading the file twice. > > Thanks. > > -- > John A. Grant * I speak only for myself * (remove 'z' to reply) > Radiation Geophysics, Geological Survey of Canada, Ottawa > If you followup, please do NOT e-mail me a copy: I will read it here > > >

« previous php.general (#70032) next »