file_get_contents VS fread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • howa

    #1

    file_get_contents VS fread

    are there any advantage in replacing all fread() operations with
    file_get_conten ts() ?

    i.e.

    file_get_conten ts("/usr/local/something.txt")

    VS

    $filename = "/usr/local/something.txt";
    $handle = fopen($filename , "r");
    $contents = fread($handle, filesize($filen ame));
    fclose($handle) ;

    is that file_get_conten ts() is more efficient?

    thanks.

  • BinnyVA

    #2
    Re: file_get_conten ts VS fread

    Hi,
    are there any advantage in replacing all fread() operations with
    file_get_conten ts() ?
    You need the handle provided by fopen() for many operations
    like fgetcsv().

    Another advantage of fread() is that you can read just a said
    amount of bytes from a line - you don't have to read the full
    file at once.

    But if you just need the full contents, file_get_conten ts()
    is an easier option.

    --
    Binny V A
    Bin-Co is a site that provides some free and great codes for some of the programming languages. These include Open Sources for JavaScript, Perl, Tcl/Tk and others.


    Comment

    • Jerry Stuckle

      #3
      Re: file_get_conten ts VS fread

      howa wrote:
      are there any advantage in replacing all fread() operations with
      file_get_conten ts() ?
      >
      i.e.
      >
      file_get_conten ts("/usr/local/something.txt")
      >
      VS
      >
      $filename = "/usr/local/something.txt";
      $handle = fopen($filename , "r");
      $contents = fread($handle, filesize($filen ame));
      fclose($handle) ;
      >
      is that file_get_conten ts() is more efficient?
      >
      thanks.
      >
      Like almost anything else in programming, "it depends".

      file_get_conten ts() can be faster because it's a single call to read
      the file. But it can also be slower - because it reads the entire
      file into memory at one time.

      If you're reading 5MB files, file_get_conten ts() will take something
      more than 5MB of RAM. Stack a few of these up and you'll be using
      a LOT of RAM - maybe too much and the system can start paging.

      fread(), OTOH, only gets small amounts of data at a time. And yes,
      for large files it can take longer than file_get_conten ts(), but for
      small files I don't think you'll notice any difference.

      If you're not having performance problems, I'd say don't worry about it.

      If you are having performance problems, I suggest you figure out where
      the real bottleneck is. I doubt it's in the use of fread().

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • howa

        #4
        Re: file_get_conten ts VS fread


        Jerry Stuckle ¼g¹D¡G
        howa wrote:
        are there any advantage in replacing all fread() operations with
        file_get_conten ts() ?

        i.e.

        file_get_conten ts("/usr/local/something.txt")

        VS

        $filename = "/usr/local/something.txt";
        $handle = fopen($filename , "r");
        $contents = fread($handle, filesize($filen ame));
        fclose($handle) ;

        is that file_get_conten ts() is more efficient?

        thanks.
        >
        Like almost anything else in programming, "it depends".
        >
        file_get_conten ts() can be faster because it's a single call to read
        the file. But it can also be slower - because it reads the entire
        file into memory at one time.
        >
        If you're reading 5MB files, file_get_conten ts() will take something
        more than 5MB of RAM. Stack a few of these up and you'll be using
        a LOT of RAM - maybe too much and the system can start paging.
        >
        fread(), OTOH, only gets small amounts of data at a time. And yes,
        for large files it can take longer than file_get_conten ts(), but for
        small files I don't think you'll notice any difference.
        >
        If you're not having performance problems, I'd say don't worry about it.
        >
        If you are having performance problems, I suggest you figure out where
        the real bottleneck is. I doubt it's in the use of fread().
        >
        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===
        well, if the file size is usually less than 100K, and i need to read
        them all once, so file_get_conten ts() is preferred?

        Comment

        • Jerry Stuckle

          #5
          Re: file_get_conten ts VS fread

          howa wrote:
          Jerry Stuckle ¼g¹D¡G
          >
          >
          >>howa wrote:
          >>
          >>>are there any advantage in replacing all fread() operations with
          >>>file_get_con tents() ?
          >>>
          >>>i.e.
          >>>
          >>>file_get_con tents("/usr/local/something.txt")
          >>>
          >>>VS
          >>>
          >>>$filename = "/usr/local/something.txt";
          >>>$handle = fopen($filename , "r");
          >>>$contents = fread($handle, filesize($filen ame));
          >>>fclose($hand le);
          >>>
          >>>is that file_get_conten ts() is more efficient?
          >>>
          >>>thanks.
          >>>
          >>
          >>Like almost anything else in programming, "it depends".
          >>
          >>file_get_cont ents() can be faster because it's a single call to read
          >>the file. But it can also be slower - because it reads the entire
          >>file into memory at one time.
          >>
          >>If you're reading 5MB files, file_get_conten ts() will take something
          >>more than 5MB of RAM. Stack a few of these up and you'll be using
          >>a LOT of RAM - maybe too much and the system can start paging.
          >>
          >>fread(), OTOH, only gets small amounts of data at a time. And yes,
          >>for large files it can take longer than file_get_conten ts(), but for
          >>small files I don't think you'll notice any difference.
          >>
          >>If you're not having performance problems, I'd say don't worry about it.
          >>
          >>If you are having performance problems, I suggest you figure out where
          >>the real bottleneck is. I doubt it's in the use of fread().
          >>
          >>--
          >>============= =====
          >>Remove the "x" from my email address
          >>Jerry Stuckle
          >>JDS Computer Training Corp.
          >>jstucklex@att global.net
          >>============= =====
          >
          >
          well, if the file size is usually less than 100K, and i need to read
          them all once, so file_get_conten ts() is preferred?
          >
          If you need to read them all at once, then fine. There won't be a lot
          of memory difference between reading everything in one
          file_get_conten ts() and multiple fread()s.

          The question is - do you? Are you operating on the entire file at one
          time? Most cases unless I'm actually writing a file to the browser I
          find I'm only working on a small piece at a time

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • Andy Hassall

            #6
            Re: file_get_conten ts VS fread

            On 7 Nov 2006 00:28:06 -0800, "howa" <howachen@gmail .comwrote:
            >are there any advantage in replacing all fread() operations with
            >file_get_conte nts() ?
            >
            >i.e.
            >
            >file_get_conte nts("/usr/local/something.txt")
            >
            >VS
            >
            >$filename = "/usr/local/something.txt";
            >$handle = fopen($filename , "r");
            >$contents = fread($handle, filesize($filen ame));
            >fclose($handle );
            >
            >is that file_get_conten ts() is more efficient?
            The main difference between a complete file read using fread and
            file_get_conten ts is that file_get_conten ts 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_conten ts() 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_conten ts
            fread 1562/s -- -9%
            file_get_conten ts 1724/s 10% --

            --
            Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
            http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

            Comment

            Working...