fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. class Test {
  6. int TestList(int count) {
  7. List<int> list = new List<int>();
  8. for (int i=0; i<count; i++) list.Add(count-i);
  9. return list.Count;
  10. }
  11. int TestList2(int count) {
  12. List<int> list = new List<int>();
  13. for (int i=0; i<count; i++) list.Insert(0, i);
  14. return list.Count;
  15. }
  16. int TestLinkedList(int count) {
  17. LinkedList<int> list = new LinkedList<int>();
  18. for (int i=0; i<count; i++) list.AddLast(count-i);
  19. return list.Count;
  20. }
  21. int TestLinkedList2(int count) {
  22. LinkedList<int> list = new LinkedList<int>();
  23. for (int i=0; i<count; i++) list.AddFirst(i);
  24. return list.Count;
  25. }
  26. void Start() {
  27. int loopcnt = 1000000;
  28. int loopcnt2 = 100000;
  29. int c;
  30. Stopwatch sw = new Stopwatch();
  31. sw.Start();
  32. c = TestList(loopcnt);
  33. sw.Stop();
  34. Console.WriteLine("List back : {0} : {1}", c, sw.Elapsed);
  35. sw.Reset(); sw.Start();
  36. c = TestList2(loopcnt2);
  37. sw.Stop();
  38. Console.WriteLine("List front : {0} : {1}", c, sw.Elapsed);
  39. sw.Reset(); sw.Start();
  40. c = TestLinkedList(loopcnt);
  41. sw.Stop();
  42. Console.WriteLine("LinkedList back : {0} : {1}", c, sw.Elapsed);
  43. sw.Reset(); sw.Start();
  44. c = TestLinkedList2(loopcnt);
  45. sw.Stop();
  46. Console.WriteLine("LinkedList front : {0} : {1}", c, sw.Elapsed);
  47. }
  48. static void Main() {
  49. new Test().Start();
  50. }
  51. }
  52.  
Success #stdin #stdout 4.32s 84352KB
stdin
Standard input is empty
stdout
List back : 1000000 : 00:00:00.0209179
List front : 100000 : 00:00:03.6538403
LinkedList back : 1000000 : 00:00:00.2210205
LinkedList front : 1000000 : 00:00:00.3238953