Measuring Performance of an Extension Method. Is this right?
<p>I'm testing the efficiency of an extension method to see which permutation would be the fastest in terms of processing time. Memory consumption isn't an issue at this point.. </p>

<p>I've created a small console app to generate an array of of random strings, which then has the extension methods applied to it. I'm currently using the StopWatch class to measure the time taken to run the extension methods. I then average to total time of each method over a number of iterations.</p>

<p>I'm not excluding highest or lowest results at this point.  </p>

<p><strong>Extension Methods being tested:</strong></p>

<pre><code>public static String ToString1(this String[] s) {
    StringBuilder sb = new StringBuilder();
    foreach (String item in s) {
        sb.AppendLine(item);
    }
    return sb.ToString();
}

public static String ToString2(this String[] s) {            
    return String.Join("\n", s);
}
</code></pre>

<p><strong>Program.cs</strong> </p>

<pre><code>    static void Main(string[] args)
{
    long s1Total = 0;
    long s2Total = 0;
    double s1Avg = 0;
    double s2Avg = 0;
    int iteration = 1;
    int size = 100000;

    while (iteration &lt;= 25)
    {
        Console.WriteLine("Iteration: {0}", iteration);
        Test(ref s1Total, ref s2Total, ref iteration, size);
    }

    s1Avg = s1Total / iteration;
    s2Avg = s2Total / iteration;

    Console.WriteLine("Version\t\tTotal\t\tAvg");
    Console.WriteLine("StringBuilder\t\t{0}\t\t{1}",s1Total, s1Avg);
    Console.WriteLine("String.Join:\t\t{0}\t\t{1}",s2Total, s2Avg);

    Console.WriteLine("Press any key..");
    Console.ReadKey();
}

private static void Test(ref long s1Total, ref long s2Total, ref int iteration, int size)
{
   String[] data = new String[size];
   Random r = new Random();

   for (int i = 0; i &lt; size; i++)
   {
       data[i] = r.NextString(50);
   }

   Stopwatch s = new Stopwatch();

   s.Start();
   data.ToString1();
   s.Stop();
   s1Total += s.ElapsedTicks;

   s.Reset();

   s.Start();
   data.ToString2();
   s.Stop();
   s2Total += s.ElapsedTicks;

   iteration++;
}
</code></pre>

<p>Other extensions methods used in the above code for completeness.. </p>

<p><strong>Random extension:</strong></p>

<pre><code>    public static String NextString(this Random r,int size) 
{
    return NextString(r,size,false);
}

public static String NextString(this Random r,int size, bool lowerCase) 
{         
    StringBuilder sb = new StringBuilder();            
    char c;
    for (int i = 0; i &lt; size; i++) 
    { 
        c = Convert.ToChar(Convert.ToInt32(Math.Floor(26*r.NextDouble() + 65)));
        sb.Append(c);
    }
    if (lowerCase) {
        return sb.ToString().ToLower();
    }
    return sb.ToString();        
}
</code></pre>

<p>Running the above code, my results indicate that the StringBuilder based method is faster than String.Join based method.</p>

<p><strong>My Questions:</strong> 
Is this the right way to be performing this type of measurement.. 
Is there a better way of doing this? 
Are my results in this instance correct, and if so is using a StringBuilder actually faster than String.Join in this situation? </p>

<p>Thanks.  </p>
