stringstream (binary) performance problem

  • Follow


(first sorry for my bad english)

i have a big performance problem with following (the file is 354KB):

my environment:
  vc6 sp5
  2.4 ghz P4
  512 MB RAM

code:

fstream f("file",ios_base::in | ios_base::out | ios_base::binary);
stringstream ss;
ss << f.rdbuf();

this code use 6 seconds to finish !! this is very very slow.

i tryed many different methods:

1)

  fstream f("file",ios_base::in | ios_base::out | ios_base::binary);
  istream::pos_type size = stream_size(f);

  string buffer;
  buffer.reserve(size);
  istream_iterator<char> f_iter(f);
  copy(f_iter, istream_iterator<char>(),buffer.begin());

this use 0.057 seconds

2)

  fstream f("file",ios_base::in | ios_base::out | ios_base::binary);
  vector<char> buffer;
  istream_iterator<char> f_iter(f);
  copy(f_iter, istream_iterator<char>(),back_insert_iterator< vector<char>
>( buffer ));

this use 0.072

3) the fastest

  fstream f("file",ios_base::in | ios_base::out | ios_base::binary);
  istream::pos_type size = stream_size(f);
  char* buffer = new char[size];
  f.read(buffer,size);
  delete [] buffer;

this use 0.007 seconds.

i found that the performance problem is the minimum allocation-size in the
implementation of basic_streambuf from Dinkumware. This is 32 byte !? when i
use a my patched streambuf (65535) and insert this in the stringstream then
the first example use 0.055 second !

my question:

1) what is the "correct" and "stl-like"  way to solve this performance
problem without changing the interface of the sstream ? ( i don't wan't use
static array c-style)
2) is the following code correct to inject the patched streambuf ?

  basic_stringbufx< char, char_traits<char>, allocator<char> > strbufx;
  stringstream ss;
  ss.ostream::rdbuf(&strbufx);
  ss << f.rdbuf();

thanks for your input

Remo Eichenberger

btw. the vc7.1 - compiler optimize the first sample and has 0.01 second !!
:-)



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Remo 9/13/2003 11:48:23 PM

Remo Eichenberger wrote:
> (first sorry for my bad english)
>
> i have a big performance problem with following (the file is 354KB):
>
> my environment:
>   vc6 sp5
>   2.4 ghz P4
>   512 MB RAM
>
> code:
>
> fstream f("file",ios_base::in | ios_base::out | ios_base::binary);
> stringstream ss;
> ss << f.rdbuf();
>
> this code use 6 seconds to finish !! this is very very slow.
[SNIP]
> 1) what is the "correct" and "stl-like"  way to solve this performance
> problem without changing the interface of the sstream ? ( i don't
> wan't use static array c-style)

IMO first is to go to the Dinkumware site and check out if your header is
patched:
http://www.dinkumware.com/vc_fixes.html

Look for fstream and see the fix for a _performance_ issue.  It might be the
thing you are looking for.

I think it is worth to read through that page (if you still have to use a
compiler from the previous millenia ;-) ) and apply those fixes which might
matter.

-- 
WW aka Attila



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply White 9/14/2003 1:41:13 AM


 > this code use 6 seconds to finish !! this is very very slow.

I think your basic problem is the slow stringstream. Go to
http://www.dinkumware.com/vc_fixes.html

and apply the sstream patch (and apply the others).

Note: These patches never made it into any of Microsoft's SP's for VC++ 6.0

For VC++ 7.1, Microsoft have had a change of policy and any fixes to
Dinkumware's files will be in SP's for 7.1.
That is better news for C++ programmers using this compiler.

Stephen Howe



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Stephen 9/14/2003 11:23:04 AM

Remo Eichenberger wrote:
[bad performance of VC6's native stringstreams]

Try switching to STLport(www.stlport.org), it is free and much more
performant, and it has an active forum where you can get help with
problems.

See also the thread "how do i create a ostringstream with pre-allocated
memory buffer", it also offers a possible solution to your problem.

Uli

-- 
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ulrich 9/14/2003 11:24:00 AM

In article <3f63f2a0$0$272$cc9e4d1f@news.dial.pipex.com>, Stephen Howe 
<NOSPAMsjhowe@dial.pipex.com> writes
>I think your basic problem is the slow stringstream. Go to
>http://www.dinkumware.com/vc_fixes.html
>
>and apply the sstream patch (and apply the others).
>
>Note: These patches never made it into any of Microsoft's SP's for VC++ 6.0
>
>For VC++ 7.1, Microsoft have had a change of policy and any fixes to
>Dinkumware's files will be in SP's for 7.1.
>That is better news for C++ programmers using this compiler.

You imply that the failure to provide updates to the Library files was a 
matter of policy. I am all in favour of placing blame on implementors 
when they deserve it but as I understand it neither Dinkumware nor 
Microsoft were responsible for the failure to include updates to the 
library in VC++ 6.0. For most of the period covered by VC++ 6.0 there 
was legal action involving Dinkumware but not MS one consequence of 
which was that Dinkumware was prohibited from licensing updates for 
redistribution. MS was an innocent bystander.


-- 
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Francis 9/14/2003 1:33:05 PM

"White Wolf" <wolof@freemail.hu> wrote in message news:<bk0ans$lke$1@phys-news1.kolumbus.fi>...
 > Remo Eichenberger wrote:
 > > (first sorry for my bad english)
 > >
 > > i have a big performance problem with following (the file is 354KB):
 > >
 > > my environment:
 > >   vc6 sp5
 > >   2.4 ghz P4
 > >   512 MB RAM
 > >
 > > code:
 > >
 > > fstream f("file",ios_base::in | ios_base::out | ios_base::binary);
 > > stringstream ss;
 > > ss << f.rdbuf();
 > >
 > > this code use 6 seconds to finish !! this is very very slow.
 >  [SNIP]
 > > 1) what is the "correct" and "stl-like"  way to solve this performance
 > > problem without changing the interface of the sstream ? ( i don't
 > > wan't use static array c-style)
 >
 > IMO first is to go to the Dinkumware site and check out if your header is
 > patched:
 > http://www.dinkumware.com/vc_fixes.html
 >
 > Look for fstream and see the fix for a _performance_ issue.  It might be the
 > thing you are looking for.
 >
 > I think it is worth to read through that page (if you still have to use a
 > compiler from the previous millenia ;-) ) and apply those fixes which might
 > matter.

hi

thanks for info, but i have allready tryed the newest dinkumware-stl for vc6
(3.08). but this don't fix the problem. i found another performance problem
in ostream<<(streambuf*). this use  sgetc/sputc and not sgetn/sputn.

Remo

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply freeman_new2001 9/17/2003 9:43:23 AM

5 Replies
786 Views

(page loaded in 0.104 seconds)

Similiar Articles:











7/20/2012 10:52:21 PM


Reply: