C#中使用泛型对比使用通用基础类型效率降低近一倍



C#中使用泛型对比使用通用基础类型效率降低近一倍


下面是测试结果:

CSharp class and generic TotalMilliseconds: 270772.9229
CSharp generic TotalMilliseconds: 269963.3999
CSharp normal TotalMilliseconds: 159716.9094


测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static class InsertionSort<T> where T : IComparable
        {
            public static void Sort(T[] a)
            {
                for (var i = 0 + 1; i <= a.Length - 1; i++)
                {
                    var tmp = a[i];
                    int j = i;

                    while (j > 0 && a[j - 1].CompareTo(tmp) > 0)
                        a[j] = a[--j];

                    a[j] = tmp;
                }
            }
        }

        public static void CSharpInsertionSortGeneric<T>(T[] a) where T : IComparable
        {
            for (var i = 0 + 1; i <= a.Length - 1; i++)
            {
                var tmp = a[i];
                int j = i;

                while (j > 0 && a[j - 1].CompareTo(tmp) > 0)
                    a[j] = a[--j];

                a[j] = tmp;
            }
        }

        public static void CSharpInsertionSort(int[] a)
        {
            for (var i = 0 + 1; i <= a.Length - 1; i++)
            {
                var tmp = a[i];
                int j = i;

                while (j > 0 && a[j - 1].CompareTo(tmp) > 0)
                    a[j] = a[--j];

                a[j] = tmp;
            }
        }

        static void Main(string[] args)
        {
            DateTime start = DateTime.Now;
            int[] a = new int[200000];
            for (int i = 0; i < 200000; i++)
            {
                a[i] = 100 - i;
            }
            InsertionSort<int>.Sort(a);
            DateTime end = DateTime.Now;
            Console.WriteLine(string.Format("CSharp class and generic TotalMilliseconds: {0}", (end - start).TotalMilliseconds));

            int[] a2 = new int[200000];
            for (int i = 0; i < 200000; i++)
            {
                a2[i] = 100 - i;
            }
            CSharpInsertionSortGeneric(a2);
            DateTime end2 = DateTime.Now;
            Console.WriteLine(string.Format("CSharp generic TotalMilliseconds: {0}", (end2 - end).TotalMilliseconds));

            int[] a3 = new int[200000];
            for (int i = 0; i < 200000; i++)
            {
                a3[i] = 100 - i;
            }
            CSharpInsertionSort(a3);
            DateTime end3 = DateTime.Now;
            Console.WriteLine(string.Format("CSharp normal TotalMilliseconds: {0}", (end3 - end2).TotalMilliseconds));


            //Console.WriteLine(String.Join(" ", a));
            Console.ReadKey();
        }
    }
}


很显然,泛型降低了效率但提高了灵活性。

但是从MSDN上https://msdn.microsoft.com/en-us/library/ms172192.aspx 看到泛型有个有优点:

Better performance. Generic collection types generally perform better for storing and manipulating value types because there is no need to box the value types.

性能很好?很怀疑这不是每种情况下都是性能很好!为什么会这样?可能是运行时泛型会做更多的内存操作由此产生了效率的降低。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值