using System; using System.Diagnostics; using System.Text; using System.Threading; namespace TestConsole { internal class Program { private static void Main(string[] args) { StringFormatTest(); } private static void StringBuilderTest() { int numIterations = 100000; // number of iterations to run int maxConcats = 15; string strVal = "01234567890"; // string strVal = "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890"; //string strVal = "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890" // + "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890" // + "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890" // + "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890" // + "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890" // + "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890" // + "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890" // + "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890" // + "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890"; Thread.CurrentThread.Priority = ThreadPriority.Highest; Console.WriteLine("Traditional + concatenation"); for (int numConcats = 1; numConcats <= maxConcats; numConcats++) { Stopwatch w = new Stopwatch(); w.Start(); for (int iter = 0; iter < numIterations; iter++) { string s = strVal; for (int k = 0; k < numConcats; k++) { s = s + strVal; } } w.Stop(); Console.WriteLine(" {0}", w.ElapsedMilliseconds); } Console.WriteLine("StringBuilder()"); for (int numConcats = 1; numConcats <= maxConcats; numConcats++) { Stopwatch w = new Stopwatch(); w.Start(); for (int iter = 0; iter < numIterations; iter++) { StringBuilder s = new StringBuilder(strVal); for (int k = 0; k < numConcats; k++) { s.Append(strVal); } } w.Stop(); Console.WriteLine(" {0}", w.ElapsedMilliseconds); } Console.WriteLine("StringBuilder(full capacity)"); for (int numConcats = 1; numConcats <= maxConcats; numConcats++) { Stopwatch w = new Stopwatch(); w.Start(); for (int iter = 0; iter < numIterations; iter++) { StringBuilder s = new StringBuilder(strVal, strVal.Length*(1 + numConcats)); for (int k = 0; k < numConcats; k++) { s.Append(strVal); } } w.Stop(); Console.WriteLine(" {0}", w.ElapsedMilliseconds); Thread.CurrentThread.Priority = ThreadPriority.Normal; } //Console.WriteLine("StringBuilder(full capacity * 2)"); //for (int numConcats = 1; numConcats <= maxConcats; numConcats++) //{ // Stopwatch w = new Stopwatch(); // w.Start(); // for (int iter = 0; iter < numIterations; iter++) // { // StringBuilder s = new StringBuilder(strVal, (int) (strVal.Length*(1 + numConcats)*2)); // for (int k = 0; k < numConcats; k++) // { // s.Append(strVal); // } // } // w.Stop(); // Console.WriteLine(" {0}", w.ElapsedMilliseconds); // Thread.CurrentThread.Priority = ThreadPriority.Normal; //} //Console.WriteLine("StringBuilder(full capacity / 2)"); //for (int numConcats = 1; numConcats <= maxConcats; numConcats++) //{ // Stopwatch w = new Stopwatch(); // w.Start(); // for (int iter = 0; iter < numIterations; iter++) // { // StringBuilder s = new StringBuilder(strVal, (int) (strVal.Length*(1 + numConcats)/2)); // for (int k = 0; k < numConcats; k++) // { // s.Append(strVal); // } // } // w.Stop(); // Console.WriteLine(" {0}", w.ElapsedMilliseconds); // Thread.CurrentThread.Priority = ThreadPriority.Normal; //} } private static void StringConcatTest() { { string s = "a" + "b" + "c"; } { string s = string.Concat("a", "b", "c"); } { string s = "a"; s = s + "b"; s = s + "c"; } int numIterations = 100000; // number of iterations to run Thread.CurrentThread.Priority = ThreadPriority.Highest; string strValue = "0123456789"; { Console.WriteLine("+ concatenation"); Stopwatch w = new Stopwatch(); w.Start(); for (int iter = 0; iter < numIterations; iter++) { string s = strValue + strValue + strValue + strValue + strValue + strValue + strValue + strValue + strValue + strValue + strValue; } w.Stop(); Console.WriteLine(" {0}", w.ElapsedMilliseconds); } { Console.WriteLine("string.Concat concatenation"); Stopwatch w = new Stopwatch(); w.Start(); for (int iter = 0; iter < numIterations; iter++) { string s = string.Concat(strValue , strValue , strValue , strValue , strValue , strValue , strValue , strValue , strValue , strValue , strValue); } w.Stop(); Console.WriteLine(" {0}", w.ElapsedMilliseconds); } Thread.CurrentThread.Priority = ThreadPriority.Normal; } private static void StringFormatTest() { int numIterations = 100000; // number of iterations to run Thread.CurrentThread.Priority = ThreadPriority.Highest; string strVal = "0123456789"; { Console.WriteLine("+ concatenation"); Stopwatch w = new Stopwatch(); w.Start(); for (int iter = 0; iter < numIterations; iter++) { // code being measured: string s = string.Format("Value: {0}", strVal); } w.Stop(); Console.WriteLine(" {0}", w.ElapsedMilliseconds); } { Console.WriteLine("string.Concat concatenation"); Stopwatch w = new Stopwatch(); w.Start(); for (int iter = 0; iter < numIterations; iter++) { // code being measured string s = "Value: " + strVal; } w.Stop(); Console.WriteLine(" {0}", w.ElapsedMilliseconds); } { Console.WriteLine("+ concatenation"); Stopwatch w = new Stopwatch(); w.Start(); for (int iter = 0; iter < numIterations; iter++) { // code being measured: StringBuilder s = new StringBuilder(); s.AppendFormat("Value: {0}", strVal); } w.Stop(); Console.WriteLine(" {0}", w.ElapsedMilliseconds); } { Console.WriteLine("string.Concat concatenation"); Stopwatch w = new Stopwatch(); w.Start(); for (int iter = 0; iter < numIterations; iter++) { StringBuilder s = new StringBuilder(); s.Append("Value: " + strVal); } w.Stop(); Console.WriteLine(" {0}", w.ElapsedMilliseconds); } Thread.CurrentThread.Priority = ThreadPriority.Normal; } } }