C#泛型与非泛型性能比较的实例
时间:2022-01-31 17:26:51
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次,由此可见 使用泛型要比非泛型的效率高很多。
标签:泛型,非泛型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java优化if-else代码的实战记录
2023-05-16 22:55:18
c#创建windows服务入门教程实例
2023-02-24 11:31:31
![](https://img.aspxhome.com/file/2023/1/89781_0s.png)
C#实现创建标签PDF文件的示例代码
2023-09-15 07:04:58
![](https://img.aspxhome.com/file/2023/7/80117_0s.png)
C#开发Windows服务实例之实现禁止QQ运行
2023-04-13 03:29:29
![](https://img.aspxhome.com/file/2023/6/83296_0s.jpg)
C#执行Javascript代码的几种方法总结
2023-06-12 10:06:14
详解Android自定义控件属性TypedArray以及attrs
2023-06-20 05:13:41
![](https://img.aspxhome.com/file/2023/9/62179_0s.jpg)
c# 生成随机时间的小例子
2023-10-04 02:44:29
测试springboot项目出现Test Ignored的解决
2022-06-25 08:37:02
![](https://img.aspxhome.com/file/2023/7/97817_0s.png)
实例讲解JAVA设计模式之备忘录模式
2023-08-29 16:31:19
java如何导出insert语句并生成sql脚本
2022-05-18 10:33:54
C#复制数组的两种方式及效率比较
2023-07-15 04:19:12
![](https://img.aspxhome.com/file/2023/3/66133_0s.png)
什么是Java多线程,如何实现
2022-08-29 15:20:28
![](https://img.aspxhome.com/file/2023/6/81996_0s.jpg)
Mybatis实现单个和批量定义别名typeAliases
2023-02-11 06:27:03
java开发ServiceLoader实现机制及SPI应用
2022-12-24 09:55:05
![](https://img.aspxhome.com/file/2023/8/69728_0s.png)
java数据结构之java实现栈
2023-11-25 05:32:36
java线程池:获取运行线程数并控制线程启动速度的方法
2022-06-30 23:22:57
java fastdfs客户端使用实例代码
2022-11-19 05:35:59
![](https://img.aspxhome.com/file/2023/7/79197_0s.jpg)
java实现通讯录管理系统
2021-07-02 00:19:53
![](https://img.aspxhome.com/file/2023/2/71232_0s.jpg)
Java与C++实现相同的MD5加密算法简单实例
2023-08-31 09:43:02
![](https://img.aspxhome.com/file/2023/2/60892_0s.jpg)
springboot自定义stater启动流程
2023-06-07 06:33:45
![](https://img.aspxhome.com/file/2023/5/65165_0s.png)