Doing some perf testing, String::Join vs StringBuilder::Append...

I just posted this to a newsgroup, however, it is a question I've been asking myself for a while.  When is it appropriate to use a String::Join and when to use a StringBuilder::Append?  I'm sure there are plenty of times where the StringBuilder really comes in handy.  Especially when you can't collect all of your strings together into a single array.  However, when you have the array and you need to quickly build a new joined string, String::Join comes to the rescue.  I've created a sample program along with the timings on my machine.  Here is the newsgroup article as posted:

String.Join is implemented internally by the run-time.  It is serviced by the
ConcatenateJoinHelperArray method which turns out to be extremely fast at
creating the final string as opposed to the slightly slower
StringBuilder.Append.  If you have an array of strings, I would recommend using
that with String.Join over StringBuilder.Append since String.Join is going to be
quite a bit faster.

using System;
using System.Text;

public class JoinVsBuilder {
    private static string[] strings = new string[0];

    private static void Main(string[] args) {
        int count = int.Parse(args[0]);

        strings = new string[count];
        for(int i = 0; i < count; i++) {
            strings[i] = i.ToString();
        }

        DateTime start, end;

        start = DateTime.Now;
        string newStr = string.Join("foo", strings);
        end = DateTime.Now;
        Console.WriteLine("String::Join timing is {0}", end-start);

        StringBuilder sb = new StringBuilder();
        start = DateTime.Now;

        // Faster than worrying about when to append the connector
        sb.Append(strings[0]);
        for(int i = 1; i < strings.Length; i++) {
            sb.Append("foo");
            sb.Append(strings[i]);
        }
        string newStr2 = sb.ToString();
        end = DateTime.Now;
        Console.WriteLine("StringBuilder::Append timing is {0}", end-start);
    }
}

C:\Projects\CSharp\Samples\JoinVsBuilder>JoinVsBuilder.exe 1000000
String::Join timing is 00:00:00.4606624
StringBuilder::Append timing is 00:00:02.9141904

That is pretty much all I have for right now.  Hopefully I haven't been less than rigorous in my testings.  I don't feel that I've given StringBuilder::Append a bad shake since there are proper times to use it.  Just don't use it when you already have your values ready to go since a String::Join can be quite a bit faster.

Published Thursday, March 04, 2004 11:19 PM by Justin Rogers

Comments

Saturday, March 06, 2004 12:55 AM by TrackBack

# String::Join vs StringBuilder::Append vs StringBuilder::Append /w capacity...

Sunday, March 07, 2004 1:15 AM by Richard Tallent

# re: Doing some perf testing, String::Join vs StringBuilder::Append...

This may be a bit unfair to StringBuilder: after all, it is dynamically resizing its internal structure each time you double the number of characters after 16, whereas your Array has already been instatiated, sized, and populated.

Better apples to apples:
1. Use StringBuilder.EnsureCapacity to overcome the advantage Array has by having a preset allocated size.
2. Move the starting time for Array to above the For loop to remove the bias of overhead from the loop and population effort.

Notwithstanding the above, you've made a good point: if you already have an array, String.Join makes a lot more sense than iterating through the array again and copying it to a StringBuilder.
Sunday, March 07, 2004 2:56 AM by Justin Rogers

# re: Doing some perf testing, String::Join vs StringBuilder::Append...

I accounted for the resize in a follow-up post. If you click on the trackback link for the first comment you'll see the follow-up. While there is stil some incorrect information in the follow-up regarding string interning which turns out not to be happening at all, the timing values are spot on and shows String::Join to be only 25% faster than StringBuilder::Append when using capacity planning.
Sunday, March 14, 2004 9:40 PM by TrackBack

# Testing your tests. Ensuring test validity and preventing common mistakes...

Sunday, March 14, 2004 9:42 PM by TrackBack

# Testing your tests. Ensuring test validity and preventing common mistakes...

Thursday, October 04, 2007 12:35 PM by lose weight loss workout exercise fitness exercises for

# lose weight loss workout exercise fitness exercises for

Thank you. weight-loss-pages.com

Friday, September 23, 2011 1:40 PM by Buy oem software online

# re: Doing some perf testing, String::Join vs StringBuilder::Append...

KZXLMs Not bad post, leave it at my bookmarks!...

Sunday, September 25, 2011 3:40 AM by Buy cheap OEM software

# re: Doing some perf testing, String::Join vs StringBuilder::Append...

WiRl7L The author deserves for the monument:D

Wednesday, October 26, 2011 4:05 PM by OEM software download

# re: Doing some perf testing, String::Join vs StringBuilder::Append...

10uYov Fresh thoughts, fresh view on the subject..!

Friday, October 28, 2011 12:06 PM by Cheap oem software

# re: Doing some perf testing, String::Join vs StringBuilder::Append...

Pl35fl Of course, I understand a little about this post but  will try cope with it!!...

Friday, October 28, 2011 3:00 PM by Cheap oem software

# re: Doing some perf testing, String::Join vs StringBuilder::Append...

xo3Ik7 It's pleasant sitting at work to distract from it�to relax and read the information written here:D

Friday, October 28, 2011 7:42 PM by Buy cheap OEM software online

# re: Doing some perf testing, String::Join vs StringBuilder::Append...

hzphij Extremely easy by words but in reality�, a lot of things don`t correspond. Not everything is so rosy..!

Saturday, October 29, 2011 3:15 AM by Buy oem

# re: Doing some perf testing, String::Join vs StringBuilder::Append...

d7jDui The text is promising, will place the site to my favorites..!

Leave a Comment

(required) 
(required) 
(optional)
(required)