Results 1 to 5 of 5

Thread: StringBuilder

  1. #1

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    StringBuilder

    Hi, Is the stringbuilder any better\faster at concatenating strings with
    Code:
    sb.Append("TEXT");
    sb.Append(",");
    sb.Append("Text");
    rather than
    Code:
    strText = "Text" + "," + "Text";
    I have a very long operation wich concatenates thousands of strings for inserting into a database (14 strings per record), is the string builder class going to provide any performance gains over normal string concatenation?
    String handling is the final area for me to get anymore performance gains out of it, I am already running it on a seperate thread. Also if I use SQL CE ( compact edition) will I get anymore performance out of that rather than MS Access?

    Thanks...

    Signatures suck

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: StringBuilder

    There's an overhead associated with creating and using a StringBuilder so for only a small number of concatenations it will be no faster and may actually be slower. You wouldn't notice the difference though. After about a dozen concatenations the StringBuilder will start to perform better though, and the difference will be significant with a large number of concatenations. In your case you'll likely see a big improvement.

    That said, I don't see why you're asking us when you could have tested it for yourself in a few minutes at the most.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: StringBuilder

    Thanks, actually i did test it and it did perform slower with one million concatenations, but it was using the same strings. eg

    string s = "asdf" + "," + "asdf" and so on for 14 strings;
    vs
    sb.Append("asdf");
    sb.append(",");
    sb.Append("asdf");
    and so on for 14 strings.
    both of these were in loops wich went for upto 1 million iterations.

    Signatures suck

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: StringBuilder

    Quote Originally Posted by cptHotkeys
    Thanks, actually i did test it and it did perform slower with one million concatenations, but it was using the same strings. eg

    string s = "asdf" + "," + "asdf" and so on for 14 strings;
    vs
    sb.Append("asdf");
    sb.append(",");
    sb.Append("asdf");
    and so on for 14 strings.
    both of these were in loops wich went for upto 1 million iterations.
    I don't think you've actually performed a valid test there. Like I said, the difference only starts to show after about 10 or 20 operations. You're just testing 14 operations and performing that same test over and over. You need to use a single String and StringBuilder object and perform a large number of operations on those objects. Try this code:
    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
        int iterations = 10000;
        string s = string.Empty;
        Stopwatch timer = Stopwatch.StartNew();
    
        for (int i = 0; i < iterations; i++)
        {
            s += "Hello World";
        }
    
        MessageBox.Show(timer.Elapsed.ToString(), "String");
    
        StringBuilder sb = new StringBuilder();
        timer = Stopwatch.StartNew();
    
        for (int i = 0; i < iterations; i++)
        {
            sb.Append("Hello World");
        }
    
        MessageBox.Show(timer.Elapsed.ToString(), "StringBuilder");
    }
    You'll see that both times are less than a second but the difference is about 3 orders of magnitude. Now try adding another 0 to the iterations variable. You'll see that the the first time blows out to a ridiculous value while the second is still very low. The time it takes to concatenate Strings increases geometrically because of the need to reallocate and copy increasingly large amounts of data, not to mention the increasingly large amount of memory occupied by all those discarded String objects. The time it takes to append to a StringBuilder increases pretty much linearly with the number of append operations.
    Last edited by jmcilhinney; Nov 2nd, 2008 at 10:23 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: StringBuilder

    thankyou verry much...

    Now i understand the string builder is not for me, as I will have to clear it and pass it to a function (I was using a string builder inside the classes function which returnd sb.tostring) thousands of times. and the ammount of memory required for each record is probably never more than 500 bytes (in ascii).
    From what I understand I may get a small gain out of it but not allot, I will make an overloaded method with both options to begin with i think and test it when i write a class to handle the database...

    thanks

    Signatures suck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width