Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Fastest way to read entire file into memory

Fastest way to read entire file into memory

Scheduled Pinned Locked Moved C / C++ / MFC
performance
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    georgiek50
    wrote on last edited by
    #1

    Which is currently the fastest method to read a text file into memory. I am using ifstream and get and would like to know of anything better.

    R N J 3 Replies Last reply
    0
    • G georgiek50

      Which is currently the fastest method to read a text file into memory. I am using ifstream and get and would like to know of anything better.

      R Offline
      R Offline
      Rein Hillmann
      wrote on last edited by
      #2

      Are you doing ifstream::read()? What is the length of the data block you are reading? Try increasing the size of the chunks you read if it's reading too slowly. Reading 1 byte at a time is incredibly slow but reading 1MB at a time will be really fast.

      G 1 Reply Last reply
      0
      • G georgiek50

        Which is currently the fastest method to read a text file into memory. I am using ifstream and get and would like to know of anything better.

        N Offline
        N Offline
        Neville Franks
        wrote on last edited by
        #3

        What are you doing with the file? Memory mapped files deliver impressive performance and may be suitable for your task. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

        1 Reply Last reply
        0
        • R Rein Hillmann

          Are you doing ifstream::read()? What is the length of the data block you are reading? Try increasing the size of the chunks you read if it's reading too slowly. Reading 1 byte at a time is incredibly slow but reading 1MB at a time will be really fast.

          G Offline
          G Offline
          georgiek50
          wrote on last edited by
          #4

          The length of the data block is the entire filesize (which varies)...in my case it is around 500K at the moment...the entire file is read w/ one get. It is not that slow, I just thought there may be a faster way.

          P 1 Reply Last reply
          0
          • G georgiek50

            The length of the data block is the entire filesize (which varies)...in my case it is around 500K at the moment...the entire file is read w/ one get. It is not that slow, I just thought there may be a faster way.

            P Offline
            P Offline
            palbano
            wrote on last edited by
            #5

            I have experimented with this now and again and always found that blocks of 2,4 and 8k ranges provide the best performance. I have always assumed that there must be variants across systems that might effect this. OS, File System, Disk Drive and probably more. It is relatively simple to create a console app that allows you to test variations.

            "No matter where you go, there your are." - Buckaroo Banzai

            -pete

            1 Reply Last reply
            0
            • G georgiek50

              Which is currently the fastest method to read a text file into memory. I am using ifstream and get and would like to know of anything better.

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              Hmmmm! 1) Open file. 2) Get size of file. 3) Allocate enough memory to hold entire file. 4) Read file into buffer. Of cource this only applies if file size if smaller than the maximum buffer size that you can allocate. Under Win32, you can allocate very very large buffers. INTP

              G 1 Reply Last reply
              0
              • J John R Shaw

                Hmmmm! 1) Open file. 2) Get size of file. 3) Allocate enough memory to hold entire file. 4) Read file into buffer. Of cource this only applies if file size if smaller than the maximum buffer size that you can allocate. Under Win32, you can allocate very very large buffers. INTP

                G Offline
                G Offline
                georgiek50
                wrote on last edited by
                #7

                Yep, that's exactly what I did, just making sure it's the fastest way. Thanks for the help.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups