打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#泛型与非泛型性能比较的实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication
{
    class Program
    {
        static int length = 1000 * 1000;
        static void Main(string[] args)
        {
            int iteration=10;//方法执行次数
            CodeTimer.Time("值类型处理-泛型方法", iteration, Test1);
            CodeTimer.Time("值类型处理-非泛型方法", iteration, Test2);
            //CodeTimer.Time("引用类型处理-泛型方法", iteration, Test3);
            //CodeTimer.Time("引用类型处理-非泛型方法", iteration, Test4);
            Console.ReadKey();
        }
        /// <summary>
        /// 值类型泛型方法
        /// </summary>
        static  void Test1()
        {
            List<int> l = new List<int>();
            for (int i = 0; i < length; i++)
            {
                l.Add(i);
                int a = l[i];
            }
            l = null;
        }
        /// <summary>
        /// 值类型非泛型方法
        /// </summary>
        static void Test2()
        {
            ArrayList a = new ArrayList();
            for (int i = 0; i < length; i++)
            {
                a.Add(i);
                int s = (int)a[i];
            }
            a = null;
        }
        /// <summary>
        /// 引用类型泛型方法
        /// </summary>
        static void Test3()
        {
            List<string> l = new List<string>();
            for (int i = 0; i < length; i++)
            {
                l.Add("l");
                string s = l[i];
            }
        }
        /// <summary>
        /// 引用类型的非泛型方法
        /// </summary>
        static void Test4()
        {
            ArrayList a = new ArrayList();
            for(int i=0;i<length;i++)
            {
                a.Add("a");
                string s = (string)a[i];
            }
            a = null;
        }
    }
}

值类型的泛型与非泛型的性能比较,方法执行10次,由此可见 使用泛型要比非泛型的效率高很多。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
android小知识:ArrayList和数组之间的转换
java常用集合类详解(有例子,集合类糊涂的来看!) .
java基础习题(二)
最全的Java笔试题库之选择题篇-总共234道【1~60】
C#程序设计笔试题
zookeeper sync & async & multi example and performance test
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服