All posts tagged Development

C# – String.Concat vs String.Join

I had a quick Google search for a comparison between string.concat and string.join, but I couldn’t find anything.

Say you want to construct the sentence “The quick brown fox jumps over the lazy dog”

This is comprised of 9 words, 8 spaces.

Using string.concat:

public void CreateSentanceUsingStringConcat()
{
    string space = " ";
    string the = "the";
    string quick = "quick";
    string brown = "brown";
    string fox = "fox";
    string jumps = "jumps";
    string over = "over";
    string lazy = "lazy";
    string dog = "dog";

    var result = string.Concat(the, space, quick, space, brown, space, fox, space, jumps, space, over, space, the, space, lazy, space, dog);
}

Using string.join

public void CreateSentanceUsingStringJoin()
{
    var result = string.Join(space, the, quick, brown, fox, jumps, over, the, lazy, dog);
}

With string.concat, we must explicitly add the separator (in our case, space) between each string.

String.join however, allows us to specify the separator as the first parameter.

Performance

I wrote a small benchmark test on the two (see bottom of this post for code)

We can see string.concat consistently performs better

 string.join - 516ms / string.concat - 329ms

Benchmark Code:

class Program
{
    const int m = 1000000;

    const string space = " ";
    const string the = "the";
    const string quick = "quick";
    const string brown = "brown";
    const string fox = "fox";
    const string jumps = "jumps";
    const string over = "over";
    const string lazy = "lazy";
    const string dog = "dog";

    const string expected = "the quick brown fox jumps over the lazy dog";

    static void Main()
    {
        Console.WriteLine("Pass #1");
        Go();
        Console.WriteLine();
        Console.WriteLine("Pass #2");
        Go();
        Console.WriteLine();
        Console.WriteLine("Pass #3");
        Go();

        Console.Read();
    }

    private static void Go()
    {
        Stopwatch s1 = Stopwatch.StartNew();

        for (int i = 0; i < m; i++)
            string.Concat(the, space, quick, space, brown, space, fox, space, jumps, space, over, space, the, space, lazy, space, dog);

        s1.Stop();

        Stopwatch s2 = Stopwatch.StartNew();
        for (int i = 0; i < m; i++)
            string.Join(space, the, quick, brown, fox, jumps, over, the, lazy, dog);

        s2.Stop();

        Console.WriteLine("string.join - {0}", s1.ElapsedMilliseconds);
        Console.WriteLine("string.concat- {0}", s2.ElapsedMilliseconds);
    }
}

Base checksum mismatch – A solution for TortoiseSVN users.

I recently had the misfortune to get a Base Checksum Mismatch error, when I tried to check in files to my repository.
The problem occurs when the files svn data has become corrupted.
There are many solutions to this problem, but here’s how I solved it simply (I only had 2 files that had this error)

  1. Check in everything except the files that are causing the issue.
  2. Take a fresh checkout to a separate directory
    (right click in a directory -> SVN Checkout)
    Screen should look like the image below:

    image

  3. After TortoiseSVN has checked out everything to the new directory, simply copy across your files that were giving you trouble
  4. Check back in.

That’s how I fixed it.
Hope this helps!