C#泛型與非泛型性能比較的實(shí)例
更新時(shí)間:2013年04月16日 16:59:55 作者:
C#泛型與非泛型性能比較的實(shí)例,需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
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;//方法執(zhí)行次數(shù)
CodeTimer.Time("值類(lèi)型處理-泛型方法", iteration, Test1);
CodeTimer.Time("值類(lèi)型處理-非泛型方法", iteration, Test2);
//CodeTimer.Time("引用類(lèi)型處理-泛型方法", iteration, Test3);
//CodeTimer.Time("引用類(lèi)型處理-非泛型方法", iteration, Test4);
Console.ReadKey();
}
/// <summary>
/// 值類(lèi)型泛型方法
/// </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>
/// 值類(lèi)型非泛型方法
/// </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>
/// 引用類(lèi)型泛型方法
/// </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>
/// 引用類(lèi)型的非泛型方法
/// </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;
}
}
}
值類(lèi)型的泛型與非泛型的性能比較,方法執(zhí)行10次,由此可見(jiàn) 使用泛型要比非泛型的效率高很多。
相關(guān)文章
C#開(kāi)發(fā)Winform控件之打開(kāi)文件對(duì)話框OpenFileDialog類(lèi)
這篇文章介紹了C#開(kāi)發(fā)Winform控件之打開(kāi)文件對(duì)話框OpenFileDialog類(lèi),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法
本篇文章小編為大家介紹,關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法。需要的朋友參考下2013-04-04C# double和decimal數(shù)據(jù)類(lèi)型以截?cái)嗟姆绞奖A糁付ǖ男?shù)位數(shù)
從事ASP.NET in C#開(kāi)發(fā)快一年了,今天才知道,C#中保留小數(shù)位數(shù)時(shí)沒(méi)有使用截?cái)嗟姆绞?/div> 2012-05-05C# WebApi 接口返回值不困惑:返回值類(lèi)型詳解
這篇文章主要介紹了C# WebApi 接口返回值不困惑:返回值類(lèi)型詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07最新評(píng)論