C#算法时间测试

来源:互联网 发布:康奈尔大学知乎 编辑:程序博客网 时间:2024/04/24 16:34

    C#算法时间测试类Timing:

          public class Timing

    {

        TimeSpan startingtime;

        TimeSpan duration;

        public Timing()

        {

            startingtime = new TimeSpan(0);

            duration = new TimeSpan(0);

        }

        public void stoptime()

        {

            duration = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(startingtime);

            //线程处理

        }

        public void starttime()

        {

           GC.Collect();

           GC.WaitForPendingFinalizers();//处理堆上的垃圾回收机制

           startingtime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;

        }

        public TimeSpan result()

        {

            return duration;

        }

 

        另对习题1的简单查询实现:

          SortedList mylist = new SortedList();

            mylist.Add("link", "99");

            mylist.Add("zeal","98");

            mylist.Add("jack","97");

            Console.WriteLine("please write the name you need to find:");

            int index=mylist.IndexOfKey(Console.ReadLine());

            if (index == -1)

                Console.WriteLine("the name has not found!");

            else

                Console.WriteLine("the value you find is :" + mylist.GetByIndex(index));

            Console.ReadKey();

       利用sortedlist类来实现键值的字典存储。

原创粉丝点击