code_junkie

C# String output: format or concat?

Nov 10th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. var p = new { FirstName = "Bill", LastName = "Gates" };
  2.  
  3. Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
  4.  
  5. Console.WriteLine(p.FirstName + " " + p.LastName);
  6.  
  7. string.Format
  8.  
  9. string.Format
  10.  
  11. Bill Gates
  12. Console.WriteLine(p.FirstName + " " + p.LastName); took: 8ms - 30488 ticks
  13. Bill Gates
  14. Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took: 0ms - 182 ticks
  15.  
  16. Bill Gates
  17. Console.WriteLine(FirstName + " " + LastName); took: 5ms - 20335 ticks
  18. Bill Gates
  19. Console.WriteLine(FirstName + " " + LastName); took: 0ms - 156 ticks
  20. Bill Gates
  21. Console.WriteLine(FirstName + " " + LastName); took: 0ms - 122 ticks
  22. Bill Gates
  23. Console.WriteLine("{0} {1}", FirstName, LastName); took: 0ms - 181 ticks
  24. Bill Gates
  25. Console.WriteLine("{0} {1}", FirstName, LastName); took: 0ms - 122 ticks
  26. Bill Gates
  27. String.Concat(FirstName, " ", LastName); took: 0ms - 142 ticks
  28. Bill Gates
  29. String.Concat(FirstName, " ", LastName); took: 0ms - 117 ticks
  30.  
  31. Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took (avg): 0ms - 689 ticks
  32. Console.WriteLine(p.FirstName + " " + p.LastName); took (avg): 0ms - 683 ticks
  33.  
  34. Stopwatch s = new Stopwatch();
  35.  
  36. var p = new { FirstName = "Bill", LastName = "Gates" };
  37.  
  38. //First print to remove the initial cost
  39. Console.WriteLine(p.FirstName + " " + p.LastName);
  40. Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
  41.  
  42. int n = 100000;
  43. long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0;
  44.  
  45. for (var i = 0; i < n; i++)
  46. {
  47. s.Start();
  48. Console.WriteLine(p.FirstName + " " + p.LastName);
  49. s.Stop();
  50. cElapsedMilliseconds += s.ElapsedMilliseconds;
  51. cElapsedTicks += s.ElapsedTicks;
  52. s.Reset();
  53. s.Start();
  54. Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
  55. s.Stop();
  56. fElapsedMilliseconds += s.ElapsedMilliseconds;
  57. fElapsedTicks += s.ElapsedTicks;
  58. s.Reset();
  59. }
  60.  
  61. Console.Clear();
  62.  
  63. Console.WriteLine("Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took (avg): " + (fElapsedMilliseconds / n) + "ms - " + (fElapsedTicks / n) + " ticks");
  64. Console.WriteLine("Console.WriteLine(p.FirstName + " " + p.LastName); took (avg): " + (cElapsedMilliseconds / n) + "ms - " + (cElapsedTicks / n) + " ticks");
  65.  
  66. string str = "{0} {1} is my friend. {3}, {2} is my boss.".FormatWith(prop1,prop2,prop3,prop4);
  67.  
  68. string name = String.Format(ApplicationStrings.General.InformalUserNameFormat,this.FirstName,this.LastName);
  69.  
  70. System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
  71.  
  72. var p = new { FirstName = "Bill", LastName = "Gates" };
  73.  
  74. s.Start();
  75. Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
  76. s.Stop();
  77. Console.WriteLine("Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took: " + s.ElapsedMilliseconds + "ms - " + s.ElapsedTicks + " ticks");
  78.  
  79. s.Reset();
  80. s.Start();
  81. Console.WriteLine(p.FirstName + " " + p.LastName);
  82. s.Stop();
  83.  
  84. Console.WriteLine("Console.WriteLine(p.FirstName + " " + p.LastName); took: " + s.ElapsedMilliseconds + "ms - " + s.ElapsedTicks + " ticks");
  85.  
  86. Bill Gates
  87. Console.WriteLine("{0} {1}", p.FirstName, p.LastName); took: 2ms - 7280 ticks
  88. Bill Gates
  89. Console.WriteLine(p.FirstName + " " + p.LastName); took: 0ms - 67 ticks
  90.  
  91. Console.WriteLine("User {0} accessed {1} on {2}.", user.Name, fileName, timestamp);
  92. vs
  93. Console.WriteLine("User " + user.Name + " accessed " + fileName + " on " + timestamp + ".");
  94.  
  95. s.Start();
  96. for (var i = 0; i < n; i++)
  97. result = string.Concat(p.FirstName, " ", p.LastName);
  98. s.Stop();
  99. ceElapsedMilliseconds = s.ElapsedMilliseconds;
  100. ceElapsedTicks = s.ElapsedTicks;
  101. s.Reset();
  102.  
  103. 1000000 x result = string.Format("{0} {1}", p.FirstName, p.LastName); took: 249ms - 3571621 ticks
  104. 1000000 x result = (p.FirstName + " " + p.LastName); took: 65ms - 944948 ticks
  105. 1000000 x result = string.Concat(p.FirstName, " ", p.LastName); took: 54ms - 780524 ticks
  106.  
  107. Console.WriteLine(String.Concat(p.FirstName," ",p.LastName));
  108.  
  109. class Program {
  110. static void Main(string[] args) {
  111.  
  112. var p = new { FirstName = "Bill", LastName = "Gates" };
  113.  
  114. var tests = new[] {
  115. new { Name = "Concat", Action = new Action(delegate() { string x = p.FirstName + " " + p.LastName; }) },
  116. new { Name = "Format", Action = new Action(delegate() { string x = string.Format("{0} {1}", p.FirstName, p.LastName); }) },
  117. new { Name = "StringBuilder", Action = new Action(delegate() {
  118. StringBuilder sb = new StringBuilder();
  119. sb.Append(p.FirstName);
  120. sb.Append(" ");
  121. sb.Append(p.LastName);
  122. string x = sb.ToString();
  123. }) }
  124. };
  125.  
  126. var Watch = new Stopwatch();
  127. foreach (var t in tests) {
  128. for (int i = 0; i < 5; i++) {
  129. Watch.Reset();
  130. long Elapsed = ElapsedTicks(t.Action, Watch, 10000);
  131. Console.WriteLine(string.Format("{0}: {1} ticks", t.Name, Elapsed.ToString()));
  132. }
  133. }
  134. }
  135.  
  136. public static long ElapsedTicks(Action ActionDelg, Stopwatch Watch, int Iterations) {
  137. Watch.Start();
  138. for (int i = 0; i < Iterations; i++) {
  139. ActionDelg();
  140. }
  141. Watch.Stop();
  142. return Watch.ElapsedTicks / Iterations;
  143. }
  144. }
  145.  
  146. Console.WriteLine("User {0} accessed {1} on {2}.",
  147. user.Name, fileName, timestamp);
  148.  
  149. Console.WriteLine("User {0} accessed {1} on {2}.",
  150. user.Name, fileName, timestamp);
  151.  
  152. Console.WriteLine("User " + user.Name + " accessed " + fileName +
  153. " on " + timestamp + ".");
  154.  
  155. Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
  156.  
  157. Console.WriteLine(p.FirstName + " " + p.LastName);
Add Comment
Please, Sign In to add comment