Re: String Buffer

Discussion in 'C++' started by kld, Jun 23, 2003.

  1. kld

    kld Guest

    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" <> escribió en el mensaje
    news:...
    > On 23 Jun 2003 04:39:57 -0700, (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
    kld, Jun 23, 2003
    #1
    1. Advertising

  2. "kld" <> wrote in message
    news:bd7eq5$ou1$...
    > 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
    John Harrison, Jun 24, 2003
    #2
    1. Advertising

  3. kld

    tom_usenet Guest

    On Mon, 23 Jun 2003 19:53:09 +0200, "kld" <> 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, Jun 24, 2003
    #3
  4. kld

    tom_usenet Guest

    On Tue, 24 Jun 2003 07:17:18 +0100, "John Harrison"
    <> wrote:

    >
    >"John Harrison" <> wrote in message
    >news:bd8q5t$pmq7l$...
    >>
    >> "kld" <> wrote in message
    >> news:bd7eq5$ou1$...
    >> > 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
    tom_usenet, Jun 24, 2003
    #4
  5. "kld" <> wrote in message
    news:...
    > 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
    Johan Nilsson, Jun 24, 2003
    #5
  6. * Johan Nilsson:
    |
    | "kld" <> wrote in message
    | news:...
    |> 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
    Robert Bauck Hamar, Jun 24, 2003
    #6
  7. kld <> 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'
    =?iso-8859-1?Q?Andr=E9_P=F6nitz?=, Jun 24, 2003
    #7
  8. kld

    tom_usenet Guest

    On 24 Jun 2003 03:12:48 -0700, (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, Jun 24, 2003
    #8
  9. kld

    tom_usenet Guest

    On Tue, 24 Jun 2003 10:45:07 GMT, "Johan Nilsson"
    <> wrote:

    >
    >"kld" <> wrote in message
    >news:...
    >> 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
    tom_usenet, Jun 24, 2003
    #9
  10. "tom_usenet" <> wrote in message
    news:...
    > On Tue, 24 Jun 2003 10:45:07 GMT, "Johan Nilsson"
    > <> wrote:
    >
    > >
    > >"kld" <> wrote in message
    > >news:...
    > >> 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
    Johan Nilsson, Jun 24, 2003
    #10
  11. kld

    tom_usenet Guest

    On Tue, 24 Jun 2003 14:04:09 GMT, "Johan Nilsson"
    <> 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
    tom_usenet, Jun 24, 2003
    #11
  12. kld

    kld Guest

    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" <> wrote in message news:<>...
    > "kld" <> wrote in message
    > news:...
    > > 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
    kld, Jun 24, 2003
    #12
  13. kld

    kld Guest

    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" <> wrote in message news:<>...
    > "kld" <> wrote in message
    > news:...
    > > 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
    kld, Jun 24, 2003
    #13
  14. 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
    Karl Heinz Buchegger, Jun 25, 2003
    #14
  15. kld

    tom_usenet Guest

    On 24 Jun 2003 08:04:20 -0700, (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
    tom_usenet, Jun 25, 2003
    #15
  16. 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
    Karl Heinz Buchegger, Jun 25, 2003
    #16
  17. kld

    kld Guest

    Yes, I am using VC 6.0

    (tom_usenet) wrote in message news:<>...
    > On 24 Jun 2003 03:12:48 -0700, (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
    kld, Jun 25, 2003
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arun Kumar Srinivasan
    Replies:
    2
    Views:
    11,401
    Gregory A. Swarthout
    Jan 29, 2004
  2. Raja
    Replies:
    12
    Views:
    24,379
    John Harrison
    Jun 21, 2004
  3. Replies:
    2
    Views:
    601
    sergejusz
    Mar 26, 2007
  4. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    409
    Neal Becker
    Jan 8, 2009
  5. xingye
    Replies:
    9
    Views:
    270
    Michael Lu
    Apr 19, 2004
Loading...

Share This Page