fork download
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. string[] vals = Enumerable.Range(0, 1000000).Select(n => n.ToString()).ToArray();
  10. string[] res = new string[1000000];
  11.  
  12. var watch = Stopwatch.StartNew();
  13. for(int i = 0; i < vals.Length; i++) {
  14. res[i] = string.Format("({0})", vals[i]);
  15. }
  16.  
  17. Console.WriteLine ("it took {0} ms for string.Format", watch.ElapsedMilliseconds);
  18.  
  19. watch = Stopwatch.StartNew();
  20. for (int i = 0; i < vals.Length; i++)
  21. {
  22. res[i] = "(" + vals[i] + ")";
  23. }
  24. Console.WriteLine ("it took {0} ms for " +"\'(\' + vals[i] +\')\';", watch.ElapsedMilliseconds);
  25.  
  26. watch = Stopwatch.StartNew();
  27. char[] data = new char[100];
  28. data[0] = '(';
  29. for(int i = 0; i < vals.Length; i++)
  30. {
  31. var str = vals[i];
  32. var len = str.Length;
  33. str.CopyTo(0, data, 1, len);
  34. data[len + 1] = ')';
  35. res[i] = new String(data, 0, len + 2);
  36. }
  37.  
  38. Console.WriteLine ("it took {0} ms for fancy", watch.ElapsedMilliseconds);
  39. }
  40. }
Success #stdin #stdout 1.38s 157696KB
stdin
stdout
it took 590 ms for string.Format
it took 177 ms for '(' + vals[i]  +')';
it took 181 ms for fancy