using System; using System.Collections.Generic; using System.Diagnostics;
namespaceAlgorithmTest09_实现栈Stack { classProgram { publicstaticlongTextStack(IStack<int> stack, int N) { Stopwatch t = new Stopwatch(); t.Start(); for (int i = 0; i < N; i++) { stack.Push(i); } for (int i = 0; i < N; i++) { stack.Pop(); } t.Stop(); return t.ElapsedMilliseconds; } staticvoidMain(string[] args) { #region 测试性能 //数组栈 int N = 10000000; Array1Stack<int> array1Stack = new Array1Stack<int>(N); long t1 = TextStack(array1Stack, N); Console.WriteLine("array1Stack'Time:{0}ms", t1);
//链表栈 LinkedList1Stack<int> linked = new LinkedList1Stack<int>(); long t2 = TextStack(linked, N); Console.WriteLine("linked'Time:{0}ms", t2);
//c#自带的stack Stack<int> stack = new Stack<int>(); Stopwatch t3 = new Stopwatch(); t3.Start(); for (int i = 0; i < N; i++) { stack.Push(i); } for (int i = 0; i < N; i++) { stack.Pop(); } t3.Stop(); Console.WriteLine("stack'Time:{0}ms", t3.ElapsedMilliseconds); #endregion Console.ReadKey(); } } }