View Full Version : Regarding file seeking...

07-31-2007, 09:59 PM
I've a question regarding performance and a single flat file used to store small, pipe-delimited bits of information about each client; each client is given a seperate line in the file. I was brainstorming for the best way to do this and ingenuity (or so I'm hoping it can still be called that) struck! Instead of reading the whole file into file_get_contents(), or the entire thing into an array with file(), why not set each line to the same length (60, in my case), fopen() the file into a handle, then use fseek() to read through the file up to the point that is 60 * the client's ID, then just read 60 characters and obtain the client's data this way. Now, I'm curious, would this be a faster way to do it, or does seeking still take the entire file into account?

07-31-2007, 10:08 PM
why dont you try it using this code from a previous tread...

function getmicrotime(){
list($msec,$sec)=explode(" ",microtime());
return ((float)$msec+(float)$sec);
at the top of the page and

echo"Generated in ".$ended." seconds";
at the very bottom,
code took from http://www.codingforums.com/showthread.php?t=120097 thanks to JordanW

07-31-2007, 10:55 PM
you've got two issues here, which way is fastest, and which way won't run out of memory. If the file fits in memory, then file() or file_get_contents() will be faster, simply due to the lack of function-overhead produced. If you need to read a bigger file though, you'll be screwed and that's where fread() (and related functions) come in.