fork(2) download
  1. using System;
  2. using System.Text;
  3. using System.Diagnostics;
  4.  
  5. public class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int max = 10000;
  10. for (int times = 0; times < 5; times++)
  11. {
  12. {
  13. Console.WriteLine("\ntime: {0}", (times+1).ToString());
  14. Stopwatch sw = Stopwatch.StartNew();
  15. for (int i = 0; i < max; i++)
  16. {
  17. string msg = "Your total is ";
  18. msg += "$500 ";
  19. msg += DateTime.Now;
  20. }
  21. sw.Stop();
  22. Console.WriteLine("String +\t: {0}ms", ((int)sw.ElapsedMilliseconds).ToString().PadLeft(6));
  23. }
  24.  
  25. {
  26. Stopwatch sw = Stopwatch.StartNew();
  27. StringBuilder msg = new StringBuilder();
  28. for (int j = 0; j < max; j++)
  29. {
  30. // msg.Clear(); // .NET 4.0
  31. msg.Remove(0, msg.Length);
  32. msg.Append("Your total is ");
  33. msg.Append("$500 ");
  34. msg.Append(DateTime.Now);
  35. }
  36. sw.Stop();
  37. Console.WriteLine("StringBuilder\t: {0}ms", ((int)sw.ElapsedMilliseconds).ToString().PadLeft(6));
  38. }
  39. }
  40. Console.Read();
  41. }
  42. }
  43.  
Success #stdin #stdout 0.68s 38352KB
stdin
Standard input is empty
stdout
time: 1
String +	:     68ms
StringBuilder	:     62ms

time: 2
String +	:     66ms
StringBuilder	:     62ms

time: 3
String +	:     66ms
StringBuilder	:     63ms

time: 4
String +	:     67ms
StringBuilder	:     61ms

time: 5
String +	:     65ms
StringBuilder	:     61ms