CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2006
    Posts
    41

    How to accelerate std::wofstream

    I have the following code that performs writing into std::wofstream (with conversion into UTF-8):

    std::wofstream stream;
    stream.open(L"D:\\MyFile.txt", std::ios_base::app | std::ios_base::out);

    std::locale utf8locale = <Get UTF-8 locale>;
    stream.imbue(utf8locale);

    int i;
    for (i = 0; i < 700000; ++i)
    {
    stream.write(L"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 150);
    }
    stream.close();

    This works too slowly — on computer P-4 3GHz the code (output file is 10 Mb) takes 4.1 sec.
    Is it possible to accelerate it?

    I have tried to large buffer for the stream (stream.rdbuf()->pubsetbuf(...)) — size of 200 Kb. But this gave no acceleration.

    The compiler is MS Visual Studio 2005; Windows XP

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: How to accelerate std::wofstream

    Try turning off sync_with_stdio. Might have significant impact.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Sep 2006
    Posts
    41

    Re: How to accelerate std::wofstream

    >for (i = 0; i < 700000; ++i)

    Sorry, misprint - in fact, there is '70000' in the program, not '700000" (extra zero). So the output file is, really, about 10 Mb. The time is, as I said, 4.1 sec.

  4. #4
    Join Date
    Sep 2006
    Posts
    41

    Re: How to accelerate std::wofstream

    Quote Originally Posted by treuss
    Try turning off sync_with_stdio. Might have significant impact.
    I have tried - no effect.

    BTW, when I tried to use std::ofstream instead of std::wofstream - the time was 200 ms! 20 times faster!!

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to accelerate std::wofstream

    Quote Originally Posted by KellyLynch
    I have tried - no effect.

    BTW, when I tried to use std::ofstream instead of std::wofstream - the time was 200 ms! 20 times faster!!
    Are you timing a release build or a debug build?

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: How to accelerate std::wofstream

    Not sure but try concatenating the string first and write in one go
    If there is no love sun won't shine

  7. #7
    Join Date
    Sep 2006
    Posts
    41

    Re: How to accelerate std::wofstream

    Quote Originally Posted by Paul McKenzie
    Are you timing a release build or a debug build?

    Regards,

    Paul McKenzie
    Release

  8. #8
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: How to accelerate std::wofstream


  9. #9
    Join Date
    Sep 2006
    Posts
    41

    Re: How to accelerate std::wofstream

    Quote Originally Posted by Mitsukai
    NtWriteFile? I do not think this is what I need. I need to somehow imrpove performance of std::wofstream; not just 'write blocks of bytes into a file'.

  10. #10
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: How to accelerate std::wofstream

    and how does NtWriteFile not do what u want but faster?

  11. #11
    Join Date
    Sep 2006
    Posts
    41

    Re: How to accelerate std::wofstream

    Quote Originally Posted by Mitsukai
    and how does NtWriteFile not do what u want but faster?
    NtWriteFile is writing buffer of bytes into file, right? And I need streamed output - using std::wfostream or some other stream. More exactly, I need WCHAR-based stream - because of my output file is Unicode UTF-8.

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