简单链表与泛型的效率测试

class Program
    {
        static Node GetSingleList(int length)
        {
            Node root = null;
            for (int i = 0; i < length; i++)
            {
                root = new Node { Next = root, Value = 0 };
            }

            return root;
        }
        static List<Item> GetList(int length)
        {
            return Enumerable
                .Range(0, length)
                .Select(t => new Item { Value = 0 })
                .ToList();
        }
        public class Item
        {
            public int Value;
        }

        public class Node
        {
            public Node Next;
            public int Value;
        }

        static void Main(string[] args)
        {
            int length = 1000;
            int count = 0;
            int iteration = 1;
            var root = GetSingleList(length);
            var watch1 = Stopwatch.StartNew();
            for (int t = 0; t < iteration; t++)
            {
                var node = root;
                while (node != null)
                {
                    count += node.Value;
                    node = node.Next;
                }
            }
            Console.WriteLine("{0} (Node List)", watch1.Elapsed);
            //    GC.Collect();
            var list = GetList(length);
            var watch2 = Stopwatch.StartNew();
            for (int t = 0; t < iteration; t++)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    count += list[i].Value;
                }
            }
            Console.WriteLine("{0} (List<Item>)", watch2.Elapsed);
            Console.ReadKey();
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值