Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: String Buffer (http://www.velocityreviews.com/forums/t267618-re-string-buffer.html)

kld 06-23-2003 05:53 PM

Re: String Buffer
 
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" <tom_usenet@hotmail.com> escribió en el mensaje
news:3ef6fd56.12428218@news.easynet.co.uk...
> On 23 Jun 2003 04:39:57 -0700, kepald@hotmail.com (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




John Harrison 06-24-2003 06:13 AM

Re: String Buffer
 

"kld" <kepald@jazzfree.com> wrote in message
news:bd7eq5$ou1$1@news.ya.com...
> 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



tom_usenet 06-24-2003 09:17 AM

Re: String Buffer
 
On Mon, 23 Jun 2003 19:53:09 +0200, "kld" <kepald@jazzfree.com> 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

tom_usenet 06-24-2003 09:18 AM

Re: String Buffer
 
On Tue, 24 Jun 2003 07:17:18 +0100, "John Harrison"
<john_andronicus@hotmail.com> wrote:

>
>"John Harrison" <john_andronicus@hotmail.com> wrote in message
>news:bd8q5t$pmq7l$1@ID-196037.news.dfncis.de...
>>
>> "kld" <kepald@jazzfree.com> wrote in message
>> news:bd7eq5$ou1$1@news.ya.com...
>> > 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

Johan Nilsson 06-24-2003 10:45 AM

Re: String Buffer
 

"kld" <kepald@hotmail.com> wrote in message
news:b03012da.0306240212.5f0d1325@posting.google.c 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



Robert Bauck Hamar 06-24-2003 10:46 AM

Re: String Buffer
 
* Johan Nilsson:
|
| "kld" <kepald@hotmail.com> wrote in message
| news:b03012da.0306240212.5f0d1325@posting.google.c 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


=?iso-8859-1?Q?Andr=E9_P=F6nitz?= 06-24-2003 11:33 AM

Re: String Buffer
 
kld <kepald@hotmail.com> 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'


tom_usenet 06-24-2003 11:53 AM

Re: String Buffer
 
On 24 Jun 2003 03:12:48 -0700, kepald@hotmail.com (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

tom_usenet 06-24-2003 11:56 AM

Re: String Buffer
 
On Tue, 24 Jun 2003 10:45:07 GMT, "Johan Nilsson"
<johan.nilsson@esrange.ssc.se> wrote:

>
>"kld" <kepald@hotmail.com> wrote in message
>news:b03012da.0306240212.5f0d1325@posting.google. 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

Johan Nilsson 06-24-2003 02:04 PM

Re: String Buffer
 

"tom_usenet" <tom_usenet@hotmail.com> wrote in message
news:3ef83c55.94091687@news.easynet.co.uk...
> On Tue, 24 Jun 2003 10:45:07 GMT, "Johan Nilsson"
> <johan.nilsson@esrange.ssc.se> wrote:
>
> >
> >"kld" <kepald@hotmail.com> wrote in message
> >news:b03012da.0306240212.5f0d1325@posting.google. 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




All times are GMT. The time now is 08:19 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.