Skip to main content
Added StringBuilder test and test run output
Source Link
mikeschuld
  • 964
  • 1
  • 11
  • 25
   Stopwatch s = new Stopwatch();

    int n = 1000000;
    long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0;

0, sbElapsedMilliseconds = 0, sbElapsedTicks = 0;

Random random = new Random(DateTime.Now.Millisecond);

    string result;
    s.Start();
    for (var i = 0; i < n; i++)
        result = (random.Next().ToString() + " " + random.Next().ToString());
    s.Stop();
    cElapsedMilliseconds = s.ElapsedMilliseconds;
    cElapsedTicks = s.ElapsedTicks;
    s.Reset();
     
s.Start();
    for (var i = 0; i < n; i++)
        result = string.Format("{0} {1}", random.Next().ToString(), random.Next().ToString());
    s.Stop();
fElapsedMilliseconds = s.ElapsedMilliseconds;
fElapsedTicks = fElapsedMillisecondss.ElapsedTicks;
s.Reset();

StringBuilder sb = new StringBuilder();
s.ElapsedMilliseconds;Start();
for(var i = 0; fElapsedTicksi =< sn; i++){
    sb.ElapsedTicks;Clear();
    ssb.ResetAppend(random.Next().ToString());
    sb.Append(" ");
    sb.Append(random.Next().ToString());
    Consoleresult = sb.ClearToString();
}
s.Stop();
sbElapsedMilliseconds = s.ElapsedMilliseconds;
sbElapsedTicks = s.ElapsedTicks;
s.Reset();

Console.WriteLine(n.ToString() + " x result = string.Format(\"{0} {1}\", p.FirstName, p.LastName); took: " + (fElapsedMilliseconds) + "ms - " + (fElapsedTicks) + " ticks");
    Console.WriteLine(n.ToString() + " x result = (p.FirstName + \" \" + p.LastName); took: " + (cElapsedMilliseconds) + "ms - " + (cElapsedTicks) + " ticks");
Console.WriteLine(n.ToString() + " x Consolesb.WriteLineClear("****************");sb.Append(random.Next().ToString());
  sb.Append(\" \"); sb.Append(random.Next().ToString()); result = sb.ToString(); took: " + (sbElapsedMilliseconds) + "ms - " + (sbElapsedTicks) + " ticks");
Console.WriteLine("****************");
Console.WriteLine("Press Enter to Quit");
Console.ReadLine();

Sample Output:

1000000 x result = Consolestring.ReadLineFormat("{0} {1}", p.FirstName, p.LastName); took: 513ms - 1499816 ticks
1000000 x result = (p.FirstName + " " + p.LastName); took: 393ms - 1150148 ticks
1000000 x sb.Clear();sb.Append(random.Next().ToString()); sb.Append(" "); sb.Append(random.Next().ToString()); result = sb.ToString(); took: 405ms - 1185816 ticks
   Stopwatch s = new Stopwatch();

    int n = 1000000;
    long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0;

    Random random = new Random(DateTime.Now.Millisecond);

    string result;
    s.Start();
    for (var i = 0; i < n; i++)
        result = (random.Next().ToString() + " " + random.Next().ToString());
    s.Stop();
    cElapsedMilliseconds = s.ElapsedMilliseconds;
    cElapsedTicks = s.ElapsedTicks;
    s.Reset();
    s.Start();
    for (var i = 0; i < n; i++)
        result = string.Format("{0} {1}", random.Next().ToString(), random.Next().ToString());
    s.Stop();
    fElapsedMilliseconds = s.ElapsedMilliseconds;
    fElapsedTicks = s.ElapsedTicks;
    s.Reset();


    Console.Clear();
    Console.WriteLine(n.ToString() + " x result = string.Format(\"{0} {1}\", p.FirstName, p.LastName); took: " + (fElapsedMilliseconds) + "ms - " + (fElapsedTicks) + " ticks");
    Console.WriteLine(n.ToString() + " x result = (p.FirstName + \" \" + p.LastName); took: " + (cElapsedMilliseconds) + "ms - " + (cElapsedTicks) + " ticks");
    Console.WriteLine("****************");
     Console.WriteLine("Press Enter to Quit");
    Console.ReadLine();
Stopwatch s = new Stopwatch();

int n = 1000000;
long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0, sbElapsedMilliseconds = 0, sbElapsedTicks = 0;

Random random = new Random(DateTime.Now.Millisecond);

string result;
s.Start();
for (var i = 0; i < n; i++)
    result = (random.Next().ToString() + " " + random.Next().ToString());
s.Stop();
cElapsedMilliseconds = s.ElapsedMilliseconds;
cElapsedTicks = s.ElapsedTicks;
s.Reset();
 
s.Start();
for (var i = 0; i < n; i++)
    result = string.Format("{0} {1}", random.Next().ToString(), random.Next().ToString());
s.Stop();
fElapsedMilliseconds = s.ElapsedMilliseconds;
fElapsedTicks = s.ElapsedTicks;
s.Reset();

StringBuilder sb = new StringBuilder();
s.Start();
for(var i = 0; i < n; i++){
    sb.Clear();
    sb.Append(random.Next().ToString());
    sb.Append(" ");
    sb.Append(random.Next().ToString());
    result = sb.ToString();
}
s.Stop();
sbElapsedMilliseconds = s.ElapsedMilliseconds;
sbElapsedTicks = s.ElapsedTicks;
s.Reset();

Console.WriteLine(n.ToString() + " x result = string.Format(\"{0} {1}\", p.FirstName, p.LastName); took: " + (fElapsedMilliseconds) + "ms - " + (fElapsedTicks) + " ticks");
Console.WriteLine(n.ToString() + " x result = (p.FirstName + \" \" + p.LastName); took: " + (cElapsedMilliseconds) + "ms - " + (cElapsedTicks) + " ticks");
Console.WriteLine(n.ToString() + " x sb.Clear();sb.Append(random.Next().ToString()); sb.Append(\" \"); sb.Append(random.Next().ToString()); result = sb.ToString(); took: " + (sbElapsedMilliseconds) + "ms - " + (sbElapsedTicks) + " ticks");
Console.WriteLine("****************");
Console.WriteLine("Press Enter to Quit");
Console.ReadLine();

Sample Output:

1000000 x result = string.Format("{0} {1}", p.FirstName, p.LastName); took: 513ms - 1499816 ticks
1000000 x result = (p.FirstName + " " + p.LastName); took: 393ms - 1150148 ticks
1000000 x sb.Clear();sb.Append(random.Next().ToString()); sb.Append(" "); sb.Append(random.Next().ToString()); result = sb.ToString(); took: 405ms - 1185816 ticks
Clarity, and spelling
Source Link
Ludington
  • 1k
  • 10
  • 12

Here's is the mistake everyone is making. StringStrings are immutable, this means the same tiny peicepiece of memory is used over and over in your code. AddingAdding the same two stringstrings together and creating the same new string over and over has NOagain doesn't impact on memory!!! . .Net is smart enough just to use the same memory reference. TryTherefore your code doesn't truly test the difference between the two concat methods.

Try this on for size:

   Stopwatch s = new Stopwatch();

    int n = 1000000;
    long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0;

    Random random = new Random(DateTime.Now.Millisecond);

    string result;
    s.Start();
    for (var i = 0; i < n; i++)
        result = (random.Next().ToString() + " " + random.Next().ToString());
    s.Stop();
    cElapsedMilliseconds = s.ElapsedMilliseconds;
    cElapsedTicks = s.ElapsedTicks;
    s.Reset();
    s.Start();
    for (var i = 0; i < n; i++)
        result = string.Format("{0} {1}", random.Next().ToString(), random.Next().ToString());
    s.Stop();
    fElapsedMilliseconds = s.ElapsedMilliseconds;
    fElapsedTicks = s.ElapsedTicks;
    s.Reset();


    Console.Clear();
    Console.WriteLine(n.ToString() + " x result = string.Format(\"{0} {1}\", p.FirstName, p.LastName); took: " + (fElapsedMilliseconds) + "ms - " + (fElapsedTicks) + " ticks");
    Console.WriteLine(n.ToString() + " x result = (p.FirstName + \" \" + p.LastName); took: " + (cElapsedMilliseconds) + "ms - " + (cElapsedTicks) + " ticks");
    Console.WriteLine("****************");
    Console.WriteLine("Press Enter to Quit");
    Console.ReadLine();

Here's is the mistake everyone is making. String are immutable this means the same tiny peice of memory is used over and over in your code. Adding two string together and creating the same new string over and over has NO impact on memory!!! .Net is smart enough just to use the same memory reference. Try this on for size:

   Stopwatch s = new Stopwatch();

    int n = 1000000;
    long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0;

    Random random = new Random(DateTime.Now.Millisecond);

    string result;
    s.Start();
    for (var i = 0; i < n; i++)
        result = (random.Next().ToString() + " " + random.Next().ToString());
    s.Stop();
    cElapsedMilliseconds = s.ElapsedMilliseconds;
    cElapsedTicks = s.ElapsedTicks;
    s.Reset();
    s.Start();
    for (var i = 0; i < n; i++)
        result = string.Format("{0} {1}", random.Next().ToString(), random.Next().ToString());
    s.Stop();
    fElapsedMilliseconds = s.ElapsedMilliseconds;
    fElapsedTicks = s.ElapsedTicks;
    s.Reset();


    Console.Clear();
    Console.WriteLine(n.ToString() + " x result = string.Format(\"{0} {1}\", p.FirstName, p.LastName); took: " + (fElapsedMilliseconds) + "ms - " + (fElapsedTicks) + " ticks");
    Console.WriteLine(n.ToString() + " x result = (p.FirstName + \" \" + p.LastName); took: " + (cElapsedMilliseconds) + "ms - " + (cElapsedTicks) + " ticks");
    Console.WriteLine("****************");
    Console.WriteLine("Press Enter to Quit");
    Console.ReadLine();

Strings are immutable, this means the same tiny piece of memory is used over and over in your code. Adding the same two strings together and creating the same new string over and over again doesn't impact memory. .Net is smart enough just to use the same memory reference. Therefore your code doesn't truly test the difference between the two concat methods.

Try this on for size:

   Stopwatch s = new Stopwatch();

    int n = 1000000;
    long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0;

    Random random = new Random(DateTime.Now.Millisecond);

    string result;
    s.Start();
    for (var i = 0; i < n; i++)
        result = (random.Next().ToString() + " " + random.Next().ToString());
    s.Stop();
    cElapsedMilliseconds = s.ElapsedMilliseconds;
    cElapsedTicks = s.ElapsedTicks;
    s.Reset();
    s.Start();
    for (var i = 0; i < n; i++)
        result = string.Format("{0} {1}", random.Next().ToString(), random.Next().ToString());
    s.Stop();
    fElapsedMilliseconds = s.ElapsedMilliseconds;
    fElapsedTicks = s.ElapsedTicks;
    s.Reset();


    Console.Clear();
    Console.WriteLine(n.ToString() + " x result = string.Format(\"{0} {1}\", p.FirstName, p.LastName); took: " + (fElapsedMilliseconds) + "ms - " + (fElapsedTicks) + " ticks");
    Console.WriteLine(n.ToString() + " x result = (p.FirstName + \" \" + p.LastName); took: " + (cElapsedMilliseconds) + "ms - " + (cElapsedTicks) + " ticks");
    Console.WriteLine("****************");
    Console.WriteLine("Press Enter to Quit");
    Console.ReadLine();
Source Link
Ludington
  • 1k
  • 10
  • 12

Here's is the mistake everyone is making. String are immutable this means the same tiny peice of memory is used over and over in your code. Adding two string together and creating the same new string over and over has NO impact on memory!!! .Net is smart enough just to use the same memory reference. Try this on for size:

   Stopwatch s = new Stopwatch();

    int n = 1000000;
    long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0;

    Random random = new Random(DateTime.Now.Millisecond);

    string result;
    s.Start();
    for (var i = 0; i < n; i++)
        result = (random.Next().ToString() + " " + random.Next().ToString());
    s.Stop();
    cElapsedMilliseconds = s.ElapsedMilliseconds;
    cElapsedTicks = s.ElapsedTicks;
    s.Reset();
    s.Start();
    for (var i = 0; i < n; i++)
        result = string.Format("{0} {1}", random.Next().ToString(), random.Next().ToString());
    s.Stop();
    fElapsedMilliseconds = s.ElapsedMilliseconds;
    fElapsedTicks = s.ElapsedTicks;
    s.Reset();


    Console.Clear();
    Console.WriteLine(n.ToString() + " x result = string.Format(\"{0} {1}\", p.FirstName, p.LastName); took: " + (fElapsedMilliseconds) + "ms - " + (fElapsedTicks) + " ticks");
    Console.WriteLine(n.ToString() + " x result = (p.FirstName + \" \" + p.LastName); took: " + (cElapsedMilliseconds) + "ms - " + (cElapsedTicks) + " ticks");
    Console.WriteLine("****************");
    Console.WriteLine("Press Enter to Quit");
    Console.ReadLine();
Post Made Community Wiki by Ludington