Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regarding file seeking...

    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?

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    why dont you try it using this code from a previous tread...
    PHP Code:
    <?php
    function getmicrotime(){
    list(
    $msec,$sec)=explode(" ",microtime());
    return ((float)
    $msec+(float)$sec);
    }
    $started=getmicrotime();
    ?>
    at the top of the page and
    PHP Code:
    <?php
    $ended
    =round(getmicrotime()-$started,4);
    echo
    "Generated in ".$ended." seconds";
    ?>
    at the very bottom,
    code took from Scan a Page thanks to JordanW

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •