CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 54
  1. #16
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: best way to deal with big array or vector?

    All streams are closed when their object is destroyed. Typically this happens when they go out of scope.

  2. #17
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: best way to deal with big array or vector?

    Quote Originally Posted by dukevn
    Afterward I still have to close the file, or I dont?
    It will be closed when the stream object is destroyed when it goes out of scope. Of course, if the end of the scope is not near, it is good to explicitly close it anyway.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #18
    Join Date
    Dec 2009
    Posts
    89

    Re: best way to deal with big array or vector?

    Quote Originally Posted by Lindley View Post
    All streams are closed when their object is destroyed. Typically this happens when they go out of scope.
    Quote Originally Posted by laserlight View Post
    It will be closed when the stream object is destroyed when it goes out of scope. Of course, if the end of the scope is not near, it is good to explicitly close it anyway.
    So it is fine if I use flush, but it is good (and concise) if I use close, right? Is flush more efficient than close?

  4. #19
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: best way to deal with big array or vector?

    Quote Originally Posted by dukevn
    So it is fine if I use flush, but it is good (and concise) if I use close, right? Is flush more efficient than close?
    It is up to use. I used flush() in my example because that is what std::endl does in addition to writing a newline, and my intention was to demonstrate that there is no need to flush the stream on each iteration. You just need to flush once at the end, and you may not even need to do that explicitly.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #20
    Join Date
    Dec 2009
    Posts
    89

    Re: best way to deal with big array or vector?

    Quote Originally Posted by laserlight View Post
    It is up to use. I used flush() in my example because that is what std::endl does in addition to writing a newline, and my intention was to demonstrate that there is no need to flush the stream on each iteration. You just need to flush once at the end, and you may not even need to do that explicitly.
    I did not know of flush() before your example , and I did not know that I have to (or should?) flush any stream? But I do remember that I did not have error or warning if I forgot closing a file.

  6. #21
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: best way to deal with big array or vector?

    Quote Originally Posted by dukevn View Post
    In the real input file, there are other things after the third column. Not sure about your suggestion, but I will try. Thanks.
    In that case, you would need to do the following (will work if there are other
    things after the third column ... or if there are only 3 columns):

    Code:
      while ( fin >> tempLine >> x1 >> x2 ) 
      {
          getline(fin,tempLine);
          for (int i=0;i<=(x2-x1);i++) 
          {
            ++mapTest[x1+i];
          }
      }

  7. #22
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: best way to deal with big array or vector?

    Quote Originally Posted by Philip Nicoletti
    In that case, you would need to do the following
    Good point. It may be more explanatory to use the ignore() member function though.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #23
    Join Date
    Dec 2009
    Posts
    89

    Re: best way to deal with big array or vector?

    Quote Originally Posted by Philip Nicoletti View Post
    In that case, you would need to do the following (will work if there are other
    things after the third column ... or if there are only 3 columns):

    Code:
      while ( fin >> tempLine >> x1 >> x2 ) 
      {
          getline(fin,tempLine);
          for (int i=0;i<=(x2-x1);i++) 
          {
            ++mapTest[x1+i];
          }
      }
    You got it just right Philip. I am wondering why
    Code:
      while ( fin >> tempLine >> x1 >> x2 ) {
        string temp;
        int x1, x2;
        if ( fin >> temp >> x1 >> x2 ) {
          for ( int i = x1; i <= x2; ++i ) {
            ++mapTest[i];
          }
        }
      }
    neglects the first input line, then you shed a light for me . Testing them now, and I will report back the results.

  9. #24
    Join Date
    Dec 2009
    Posts
    89

    Re: best way to deal with big array or vector?

    Quote Originally Posted by laserlight View Post
    It may be more explanatory to use the ignore() member function though.
    Would you mind giving me more explanation? How do I use ignore()?

  10. #25
    Join Date
    Dec 2009
    Posts
    89

    Re: best way to deal with big array or vector?

    Quote Originally Posted by dukevn View Post
    Testing them now, and I will report back the results.
    OK here is the reports with an input file of 1.06GB on a cluster node of 8 cores:

    - Original code: 36m2.169s
    - Improved v.1 code: 12m40.918s
    - Improved v.2 (without XMax): 11m39.172s
    - Final code v.3 (without string stream): 11m35.974s

    So there is no much difference between the last three versions (but three times as fast as the original one - a great improvement). One thing I am aiming now is how to make use of the multi-core advantage (right now the code runs only on one core), but it seems to be not that easy.

    Thanks for all of your helps.

  11. #26
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: best way to deal with big array or vector?

    Quote Originally Posted by dukevn
    Would you mind giving me more explanation? How do I use ignore()?
    Suppose according to the input format there will be a tab character between the first field and the second field. You could dispense with the temporary string variable that was used to ignore input:
    Code:
    int x1, x2;
    while (fin.ignore(1000, '\t') && (fin >> x1 >> x2))
    {
        fin.ignore(1000, '\n');
        for (int i = x1; i <= x2; ++i) 
        {
            ++mapTest[i];
        }
    }
    where 1000 is arbitrarily chosen. You could have used std::numeric_limits<std::streamsize>::max() instead.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #27
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: best way to deal with big array or vector?

    flush() is something you usually don't need to call yourself. It usually gets handled automatically. But it's available because occasionally you do need to call it explicitly.

    Multiple cores are not going to help much for file parsing. Everything bottlenecks through the disk controller anyway. Typically, the biggest multi-core gain comes when you're doing heavy mathematical computations in main memory.

  13. #28
    Join Date
    Dec 2009
    Posts
    89

    Re: best way to deal with big array or vector?

    Quote Originally Posted by laserlight View Post
    Suppose according to the input format there will be a tab character between the first field and the second field. You could dispense with the temporary string variable that was used to ignore input:
    Code:
    int x1, x2;
    while (fin.ignore(1000, '\t') && (fin >> x1 >> x2))
    {
        fin.ignore(1000, '\n');
        for (int i = x1; i <= x2; ++i) 
        {
            ++mapTest[i];
        }
    }
    where 1000 is arbitrarily chosen. You could have used std::numeric_limits<std::streamsize>::max() instead.
    Got it. Thanks laserlight.

  14. #29
    Join Date
    Dec 2009
    Posts
    89

    Re: best way to deal with big array or vector?

    Quote Originally Posted by Lindley View Post
    Multiple cores are not going to help much for file parsing. Everything bottlenecks through the disk controller anyway. Typically, the biggest multi-core gain comes when you're doing heavy mathematical computations in main memory.
    Are you saying that splitting input file to 8 chunks, processing those 8 chunks in parallel will not help at all?

  15. #30
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: best way to deal with big array or vector?

    Quote Originally Posted by dukevn View Post
    OK here is the reports with an input file of 1.06GB on a cluster node of 8 cores:

    - Original code: 36m2.169s
    - Improved v.1 code: 12m40.918s
    - Improved v.2 (without XMax): 11m39.172s
    - Final code v.3 (without string stream): 11m35.974s

    So there is no much difference between the last three versions (but three times as fast as the original one - a great improvement).
    My guess is that code like that should execute at the speed of file I/O.
    I know I can copy a 1GB file in about 30 seconds, so 11 minutes sounds like WAY too much.
    Could you comment out everything in your code except for I/O and see how long that takes?
    Do you mind posting your code and a sample data file?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured