为什么要使用泛型?泛型和非泛型对比
2016-10-20 23:08
302 查看
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 泛型和非泛型对比 { class Program { static void Main(string[] args) { testGeneric(); testNonGeneric(); Console.ReadKey(); } //测试泛型类型操作的运行时间 public static void testGeneric() { Stopwatch stopwatch = new Stopwatch(); List<int> list = new List<int>(); stopwatch.Start(); for (int i = 0; i < 10000000; i++) { list.Add(i); } stopwatch.Stop(); TimeSpan ts = stopwatch.Elapsed; string elapsedTime = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.TotalMilliseconds / 10); Console.WriteLine("泛型类型运行的时间:" + elapsedTime); } public static void testNonGeneric() { Stopwatch stopwatch = new Stopwatch(); ArrayList arraylist = new ArrayList(); stopwatch.Start(); for (int i = 0; i < 10000000; i++) { arraylist.Add(i); } stopwatch.Stop(); TimeSpan ts = stopwatch.Elapsed; string elapsetime = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("非泛型运行的时间:" + elapsetime); } } }
运行效果图:
相关文章推荐
- 为什么使用泛型
- Java Generic(-) 为什么需要使用泛型
- 为什么要使用泛型
- 为什么要使用泛型
- 泛型[0]为什么使用泛型
- 【转帖】为什么使用泛型
- 为什么要使用泛型
- 泛型是什么?为什么要使用泛型?
- The Java™ Tutorials — Generics :Why Use Generics? 为什么使用泛型
- 【JavaSE系列-基础篇6】——为什么使用泛型?
- 黑马程序员-- 高级网络编程 什么是泛型?泛型的定义?泛型如何使用?为什么要使用泛型?
- Java 容器 & 泛型:六、容器讲到为什么要使用泛型
- Java 容器 & 泛型:六、容器讲到为什么要使用泛型
- 建议20:使用泛型集合代替非泛型集合
- DataGridView使用非泛型或者未实现IBindingList接口泛型集合的列排序问题
- 为什么要使用JavaScript(中文:重思JavaScript的好与坏)中英对比版本
- 为什么要使用泛型?
- java基础入门-泛型(1)-为什么需要使用泛型?
- java泛型编程学习 笔记一:为什么要使用泛型
- 泛型是什么?为什么要使用泛型?