fork download
  1. using System.Diagnostics;
  2. using System;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. static string[][] GetRandomJaggedArray(int numberOfArrays, int numberOfElements)
  9. {
  10. string[][] temp = new string[numberOfArrays][];
  11. for (int i = 0; i < numberOfArrays; i++)
  12. {
  13. temp[i] = new string[numberOfElements];
  14. for (int i2 = 0; i2 < temp.Length; i2++)
  15. temp[i][i2] = Guid.NewGuid().ToString();
  16. }
  17.  
  18. return temp;
  19. }
  20.  
  21. static TimeSpan getElementsAtIndexLINQ(string[][] listOfArrays, int index, int count)
  22. {
  23. Stopwatch s = new Stopwatch();
  24. List<string> myListOfStrings;
  25.  
  26. s.Start();
  27. for (int i = 0; i < count; i++)
  28. myListOfStrings = listOfArrays.Select(a => a[index]).ToList();
  29. s.Stop();
  30. return s.Elapsed;
  31. }
  32.  
  33. static TimeSpan getElementsAtIndexFOR(string[][] listOfArrays, int index, int count)
  34. {
  35. Stopwatch s = new Stopwatch();
  36. s.Start();
  37. for (int i2 = 0; i2 < count; i2++ ) {
  38. List<string> myListOfStrings = new List<string>();
  39. for (int i = 0; i < listOfArrays.Length; i++)
  40. myListOfStrings.Add(listOfArrays[i][index]);
  41. }
  42. s.Stop();
  43. return s.Elapsed;
  44. }
  45.  
  46. static void Main(string[] args)
  47. {
  48. string[][] test1 = GetRandomJaggedArray(100, 1000);
  49.  
  50. string[][] test2 = GetRandomJaggedArray(100, 1000);
  51.  
  52. getElementsAtIndexFOR(test2, 1, 1);
  53.  
  54. TimeSpan t2 = getElementsAtIndexFOR(test2, 1, 10);
  55.  
  56. getElementsAtIndexLINQ(test1, 1, 1);
  57. TimeSpan t1 = getElementsAtIndexLINQ(test1, 1, 10);
  58.  
  59. Console.WriteLine("Linq method took {0} ticks to execute", t1.Ticks);
  60.  
  61. Console.WriteLine("For method took {0} ticks to execute", t2.Ticks);
  62.  
  63. }
  64. }
Success #stdin #stdout 0.26s 25512KB
stdin
Standard input is empty
stdout
Linq method took 1689 ticks to execute
For method took 535 ticks to execute