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. Help -- something faster than Getline() ??

Help -- something faster than Getline() ??

Scheduled Pinned Locked Moved C / C++ / MFC
asp-netgraphicshelpquestion
6 Posts 4 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.
  • W Offline
    W Offline
    webHamlet
    wrote on last edited by
    #1

    Hi -- I find getline() function to be extremely slow when reading data from a text file. I tried replacing getline with a homemade function called getChunk (shown below) but it's just as slow. getChunk(std::istream &in, std::string &s, const char terminator) { s.erase(s.begin(),s.end()) ; s.reserve(10) ; std::string::value_type ch ; while (in.get(ch) && ch != terminator) s.insert(s.end(),ch) ; } For context, here's the core of my program: rawData.reserve(numberRecords) ; int counter=0 ; do { counter=counter+1 ; std::vector record ; record.reserve(numberVars) ; for(int i=0; i::iterator j ; j=record.begin() ; record.erase(j, j+numberVars) ; } while (counter < numberRecords) ; numberRecords=rawData.size() ; Can anyone help me out here? Thanks, Kim Larsen

    T K 2 Replies Last reply
    0
    • W webHamlet

      Hi -- I find getline() function to be extremely slow when reading data from a text file. I tried replacing getline with a homemade function called getChunk (shown below) but it's just as slow. getChunk(std::istream &in, std::string &s, const char terminator) { s.erase(s.begin(),s.end()) ; s.reserve(10) ; std::string::value_type ch ; while (in.get(ch) && ch != terminator) s.insert(s.end(),ch) ; } For context, here's the core of my program: rawData.reserve(numberRecords) ; int counter=0 ; do { counter=counter+1 ; std::vector record ; record.reserve(numberVars) ; for(int i=0; i::iterator j ; j=record.begin() ; record.erase(j, j+numberVars) ; } while (counter < numberRecords) ; numberRecords=rawData.size() ; Can anyone help me out here? Thanks, Kim Larsen

      T Offline
      T Offline
      The NULL Developer
      wrote on last edited by
      #2

      webHamlet wrote:

      while (in.get(ch) && ch != terminator)

      it seems u r reading the data one byte at a time, which would slow down the process anyways. if i were u, i wud've read a large chunk(5-10kb maybe) of data from the file into memory buffer and then read and interpret text data from it. "Do first things first, and second things not at all." — Peter Drucker.

      A 1 Reply Last reply
      0
      • W webHamlet

        Hi -- I find getline() function to be extremely slow when reading data from a text file. I tried replacing getline with a homemade function called getChunk (shown below) but it's just as slow. getChunk(std::istream &in, std::string &s, const char terminator) { s.erase(s.begin(),s.end()) ; s.reserve(10) ; std::string::value_type ch ; while (in.get(ch) && ch != terminator) s.insert(s.end(),ch) ; } For context, here's the core of my program: rawData.reserve(numberRecords) ; int counter=0 ; do { counter=counter+1 ; std::vector record ; record.reserve(numberVars) ; for(int i=0; i::iterator j ; j=record.begin() ; record.erase(j, j+numberVars) ; } while (counter < numberRecords) ; numberRecords=rawData.size() ; Can anyone help me out here? Thanks, Kim Larsen

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #3

        Are you sure that it's the file I/O that's the problem? Maybe you (for testing purposes) should isolate the file reading from the storing of the data. And the compare file read times?

        A 1 Reply Last reply
        0
        • K kakan

          Are you sure that it's the file I/O that's the problem? Maybe you (for testing purposes) should isolate the file reading from the storing of the data. And the compare file read times?

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          Hi -- Thanks.. I'm 100% sure it's getline. Kim

          K 1 Reply Last reply
          0
          • T The NULL Developer

            webHamlet wrote:

            while (in.get(ch) && ch != terminator)

            it seems u r reading the data one byte at a time, which would slow down the process anyways. if i were u, i wud've read a large chunk(5-10kb maybe) of data from the file into memory buffer and then read and interpret text data from it. "Do first things first, and second things not at all." — Peter Drucker.

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            Thanks.. How do I do this? Could I use the read() function, or is there a way to make getline grab larger chunks? Kim

            1 Reply Last reply
            0
            • A Anonymous

              Hi -- Thanks.. I'm 100% sure it's getline. Kim

              K Offline
              K Offline
              kakan
              wrote on last edited by
              #6

              Hi. You could try to read the file using the old f-functions, like fopen, fgets, fclose. Read the data to a char[], and then assign it to the string. It might help. Principle: char buffer[300]; FILE * file = fopen("filename", "rt"); if(file != NULL) { while(fgets(buffer, 300, file) != NULL) { string = buffer; ..... Save it here } fclose(file); }

              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