Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: String Buffer

 
 
kld
Guest
Posts: n/a
 
      06-23-2003
The information that you request is not important, since the main utility of
"Stringbuffer" is to avoid the operation "string += string", this is bad
when strings are long, independently of language or compiler.

Thanks.


"tom_usenet" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
> On 23 Jun 2003 04:39:57 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (kld) wrote:
>
> >Does exists a class equivalent to Java's "StringBuffer"? I have try with
> >"stringstream", but it is far below in performance.

>
> How are you using it? What compiler version and library are you using?
>
> Sadly there is no real equivalent to StringBuffer, although
> std::string is probably the closest.
>
> What operations are you going to be performing on the string? Are you
> just appending? What types do you need to append? The best way to do
> it depends on these things...
>
> Tom



 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-24-2003

"kld" <(E-Mail Removed)> wrote in message
news:bd7eq5$ou1$(E-Mail Removed)...
> The information that you request is not important, since the main utility

of
> "Stringbuffer" is to avoid the operation "string += string", this is bad
> when strings are long, independently of language or compiler.
>
> Thanks.
>
>


string += string in C++ does exactly what StringBuffer does in Java.

john


 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      06-24-2003
On Mon, 23 Jun 2003 19:53:09 +0200, "kld" <(E-Mail Removed)> wrote:

>The information that you request is not important, since the main utility of
>"Stringbuffer" is to avoid the operation "string += string", this is bad
>when strings are long, independently of language or compiler.


How is StringBuffer.append(String) different to string += string?

There were some std::string implementations that don't grow their
capacity exponentially, but this isn't true of any recent
implementations. The algorithm used by StringBuffer.append is
identical used by that of modern std::string::append implementations.
You try to fit the other string into the available memory, and if you
can't, you reallocate your buffer to some factor multiplied by your
previous capacity.

Tom
 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      06-24-2003
On Tue, 24 Jun 2003 07:17:18 +0100, "John Harrison"
<(E-Mail Removed)> wrote:

>
>"John Harrison" <(E-Mail Removed)> wrote in message
>news:bd8q5t$pmq7l$(E-Mail Removed)...
>>
>> "kld" <(E-Mail Removed)> wrote in message
>> news:bd7eq5$ou1$(E-Mail Removed)...
>> > The information that you request is not important, since the main

>utility
>> of
>> > "Stringbuffer" is to avoid the operation "string += string", this is bad
>> > when strings are long, independently of language or compiler.
>> >
>> > Thanks.
>> >
>> >

>>
>> string += string in C++ does exactly what StringBuffer does in Java.
>>
>> john
>>

>
>Actaully I possibly don't know what I'm talking about here, too hasty.


I just looked at a StringBuffer implementation, and it uses the exact
same algorithm as most string+= implementations (e.g. a contiguous
buffer with a separate size and capacity).

Tom
 
Reply With Quote
 
Johan Nilsson
Guest
Posts: n/a
 
      06-24-2003

"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
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      06-24-2003
* Johan Nilsson:
|
| "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 ??

Or perhaps call sTmp.reserve(1000000)? I don't know how kld got his
numbers; I've got:
* java 1.4.1_02: 0.416 seconds
* g++ 3.2.3: 0.120 seconds (kld's code)

--
Robert Bauck Hamar

 
Reply With Quote
 
=?iso-8859-1?Q?Andr=E9_P=F6nitz?=
Guest
Posts: n/a
 
      06-24-2003
kld <(E-Mail Removed)> wrote:
> I totally disagree.
>
> JAVA: 27.5 seconds
> StringBuffer sTmp = new StringBuffer(1000000);


> C++: 282.3 seconds
> string sTmp;


You are cheating. In the Java version you allocate a buffer of suitable
size whereas in C++ you don't. Try 'reserve' and post the results.

Andre'

 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      06-24-2003
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
 
tom_usenet
Guest
Posts: n/a
 
      06-24-2003
On Tue, 24 Jun 2003 10:45:07 GMT, "Johan Nilsson"
<(E-Mail Removed)> wrote:

>
>"kld" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed). com...
>> 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 ??


That would make it less fair, surely? He's just using a
poor-for-append std::string implementation.

Tom
 
Reply With Quote
 
Johan Nilsson
Guest
Posts: n/a
 
      06-24-2003

"tom_usenet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 24 Jun 2003 10:45:07 GMT, "Johan Nilsson"
> <(E-Mail Removed)> wrote:
>
> >
> >"kld" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed). com...
> >> 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 ??

>
> That would make it less fair, surely? He's just using a
> poor-for-append std::string implementation.
>


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).

You might be correct about the implementation though.

// Johan


 
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