fork(1) download
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. var str = "abcdefghijklmnopqrstuvwxyz";
  11. var r = new Random();
  12. var sourceList = Enumerable.Range(0,1000000).Select(n=>str[r.Next(str.Length)].ToString()).ToArray();
  13.  
  14. var sw = new Stopwatch();
  15.  
  16. sw.Start();
  17. var outList = new List<string>();
  18. for(int i = 0; i < sourceList.Length; i++){
  19. if(sourceList[i] == "a" || sourceList[i] == "b"){
  20. outList.Add(sourceList[i]);
  21. }
  22. }
  23. sw.Stop();
  24. Console.WriteLine("for : " + sw.Elapsed);
  25.  
  26. sw.Reset();
  27. sw.Start();
  28. var outList2 = sourceList.Where(s=>s=="a" || s=="b").ToList();
  29. sw.Stop();
  30. Console.WriteLine("linq: " + sw.Elapsed);
  31. }
  32. }
Success #stdin #stdout 0.63s 65464KB
stdin
Standard input is empty
stdout
for : 00:00:00.0593953
linq: 00:00:00.2623156