Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: String Buffer

Reply
Thread Tools

Re: String Buffer

 
 
tom_usenet
Guest
Posts: n/a
 
      06-24-2003
On Tue, 24 Jun 2003 14:04:09 GMT, "Johan Nilsson"
<(E-Mail Removed)> wrote:

>Why is that? The StringBuffer is obviously initially sized to hold all
>data - so why not do the same thing with the string (but I would agree that
>calling reserve(1000000) separately on the string would be even faster, as
>someone else suggested).


Whoops, I didn't spot the size parameter passed to the StringBuffer
constructor - I assumed he was default constructing.

In that case, std::string should catch up, even with a poor
implementation.

Tom
 
Reply With Quote
 
 
 
 
kld
Guest
Posts: n/a
 
      06-24-2003
You are all the right. I do not have to use reserve, in addition my
compilation was debug. These is the results in release mode.


Test: JAVA: 32.3 seconds -- C++: 125.8 seconds

JAVA: 32.3 seconds
public static String fu()
{
StringBuffer sTmp = new StringBuffer();
for ( int i = 0; i < 1000000; i++ )
sTmp.append("0");
return sTmp.toString();
}

C++: 125.8 seconds
string fu()
{
string sTmp;
for ( int i = 0; i < 1000000; i++ )
sTmp += "0";
return sTmp;
}


"Johan Nilsson" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) ws.com>...
> "kld" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I totally disagree.
> >
> > Test: JAVA: 27.5 seconds -- C++: 282.3 seconds
> >
> > JAVA: 27.5 seconds
> > public static String fu()
> > {
> > StringBuffer sTmp = new StringBuffer(1000000);
> > for ( int i = 0; i < 1000000; i++ )
> > sTmp.append('0');
> > return sTmp.toString();
> > }
> >
> > C++: 282.3 seconds
> > string fu()
> > {
> > string sTmp;

>
> Did you try changing this to "string sTmp(1000000, ' ');" to make the
> comparison a bit fairer ??
>
> // Johan

 
Reply With Quote
 
 
 
 
kld
Guest
Posts: n/a
 
      06-24-2003
I want to say you, than I do not want to make a race java vs c++. I
only want to make a good program in c++.
Thanks.

"Johan Nilsson" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) ws.com>...
> "kld" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I totally disagree.
> >
> > Test: JAVA: 27.5 seconds -- C++: 282.3 seconds
> >
> > JAVA: 27.5 seconds
> > public static String fu()
> > {
> > StringBuffer sTmp = new StringBuffer(1000000);
> > for ( int i = 0; i < 1000000; i++ )
> > sTmp.append('0');
> > return sTmp.toString();
> > }
> >
> > C++: 282.3 seconds
> > string fu()
> > {
> > string sTmp;

>
> Did you try changing this to "string sTmp(1000000, ' ');" to make the
> comparison a bit fairer ??
>
> // Johan

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-25-2003


kld wrote:
>
> You are all the right. I do not have to use reserve, in addition my
> compilation was debug. These is the results in release mode.
>
> Test: JAVA: 32.3 seconds -- C++: 125.8 seconds
>


and now try this variation

> string fu()
> {
> string sTmp;


sTmp.reserve( 1000000 );

> for ( int i = 0; i < 1000000; i++ )
> sTmp += "0";
> return sTmp;
> }


If I do it on my compiler (VC6.0) the runtime
in relase mode drops from 86 sec to 0.06 sec

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      06-25-2003
On 24 Jun 2003 08:04:20 -0700, (E-Mail Removed) (kld) wrote:

>I want to say you, than I do not want to make a race java vs c++. I
>only want to make a good program in c++.


In that case, bear in mind that std::string doesn't necessarily have
good "growth" behaviour (although it does on implementations I've seen
other than VC6's), so make occasional manual calls to reserve to make
sure the size increases exponentially. e.g.

string fu()
{
string sTmp;
for ( int i = 0; i < 1000000; i++ )
{
if (sTmp.size() == sTmp.capacity())
{
//more than double the capacity
sTmp.reserve(4 + sTmp.capacity() * 2);
}
sTmp += "0";
}
return sTmp;
}

It's a pain, but you can avoid having to do it by upgrading your
library (either to STLport or a more recent Dinkumware library) or by
upgrading to a more recent version of MSVC (such as 7 or 7.1).

Tom
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-25-2003


kld wrote:
>
> This is an example. It can happen in real programming when the string
> become very long, by example a large dynamic html page.
> Thanks.


But I think even you would admit that adding a single
character a million times to always the same string
happens very infrequently in real programming.

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
kld
Guest
Posts: n/a
 
      06-25-2003
Yes, I am using VC 6.0

(E-Mail Removed) (tom_usenet) wrote in message news:<(E-Mail Removed)>...
> On 24 Jun 2003 03:12:48 -0700, (E-Mail Removed) (kld) wrote:
>
> >I totally disagree.
> >
> >Test: JAVA: 27.5 seconds -- C++: 282.3 seconds
> >
> >JAVA: 27.5 seconds
> >public static String fu()
> >{
> > StringBuffer sTmp = new StringBuffer(1000000);
> > for ( int i = 0; i < 1000000; i++ )
> > sTmp.append('0');
> > return sTmp.toString();
> >}
> >
> >C++: 282.3 seconds
> >string fu()
> >{
> > string sTmp;
> > for ( int i = 0; i < 1000000; i++ )
> > sTmp += '0';
> > return sTmp;
> >}

>
> Try this too:
>
> vector<char> fu()
> {
> vector<char> tmp;
> for ( int i = 0; i < 1000000; i++ )
> tmp.push_back('0');
> return tmp;
> }
>
>
> You must have an old compiler (VC6 perhaps?). Upgrade, and the C++
> version should become faster than the Java one.
>
> The problem is that VC6's std::string didn't perform exponential
> growth - it favours memory usage over speed (contrary to most users
> hopes). VC7 (and other compilers) fix this problem by using
> exponential growth (similar to StringBuffer, but in C++ hence
> faster!).
>
> Tom

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
buffer creates only read-only buffer? Neal Becker Python 0 01-08-2009 01:58 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
convert M bit buffer to N bit buffer runcyclexcski@yahoo.com C++ 2 03-26-2007 09:43 AM
How to know the buffer size and increase buffer size in c++ Raja C++ 12 06-21-2004 06:21 PM



Advertisments