源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#泛型与非泛型性能比较的实例

  • 时间:2022-02-05 18:35 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#泛型与非泛型性能比较的实例
[u]复制代码[/u] 代码如下:
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;         }     } }
[img]http://files.jb51.net/file_images/article/201304/2013416164056914.jpg[/img] 值类型的泛型与非泛型的性能比较,方法执行10次,由此可见 使用泛型要比非泛型的效率高很多。 [img]http://files.jb51.net/file_images/article/201304/2013416165703830.jpg[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部