C# 集合性能比较(代码测试)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Table_And_List
{ public class Program
{ public static void Main(string[] args)
{
int total = ;
int[] x = new int[];
for (int i = ; i < x.Length; i++)
{
x[i] = i;
}
int[] x2 = new int[] { , , , , };
int x3 = new Random().Next(total);
SortedDictionaryTest(x, x2, x3);
DictionaryTest(x, x2, x3);
SortedListTest(x, x2, x3);
HashtableTest(x, x2, x3);
Console.ReadKey();
} private static void SortedListTest(int[] x, int[] x2, int x3)
{
var ht = new SortedList();
Stopwatch watch = new Stopwatch();
watch.Start();
foreach (var item in x)
{
ht.Add(item, item);
}
watch.Stop();
Console.WriteLine(string.Format("SortedList添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds));
watch.Reset();
watch.Start();
foreach (var item in x2)
{
//Console.WriteLine("value:{0},?{1}",item,ht.ContainsKey(item));
ht.ContainsValue(item);
}
watch.Stop();
Console.WriteLine(string.Format("SortedList查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds));
ht.Clear();
}
private static void HashtableTest(int[] x, int[] x2, int x3)
{
Hashtable ht = new Hashtable();
Stopwatch watch = new Stopwatch();
watch.Start();
foreach (var item in x)
{
ht.Add(item, item);
}
watch.Stop();
Console.WriteLine(string.Format("Hashtable添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds));
watch.Reset();
watch.Start();
foreach (var item in x2)
{
//Console.WriteLine("value:{0},?{1}",item,ht.ContainsKey(item));
//ht.ContainsKey(item);
ht.ContainsValue(item);
}
watch.Stop();
Console.WriteLine(string.Format("Hashtable查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds));
ht.Clear();
} private static void DictionaryTest(int[] x, int[] x2, int x3)
{
Dictionary<int, int> ht = new Dictionary<int, int>();
Stopwatch watch = new Stopwatch();
watch.Start();
foreach (var item in x)
{
ht.Add(item, item);
}
watch.Stop();
Console.WriteLine(string.Format("Dictionary添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds));
watch.Reset();
watch.Start();
foreach (var item in x2)
{
//Console.WriteLine("value:{0},?{1}", item, ht.ContainsKey(item));
//ht.ContainsKey(item);
ht.ContainsValue(item);
}
watch.Stop();
Console.WriteLine(string.Format("Dictionary查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds));
ht.Clear();
} private static void SortedDictionaryTest(int[] x, int[] x2, int x3)
{
SortedDictionary<int, int> ht = new SortedDictionary<int, int>();
Stopwatch watch = new Stopwatch();
watch.Start();
foreach (var item in x)
{
ht.Add(item, item);
}
watch.Stop();
Console.WriteLine(string.Format("SortedDictionary添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds));
watch.Reset();
watch.Start();
foreach (var item in x2)
{
//Console.WriteLine("value:{0},?{1}", item, ht.ContainsKey(item));
//ht.ContainsKey(item);
ht.ContainsValue(item);
}
watch.Stop();
Console.WriteLine(string.Format("SortedDictionary查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds));
ht.Clear();
}
}
}

C# 集合性能比较(代码测试)的更多相关文章
- OpenCV:Mat元素访问方法、性能、代码复杂度以及安全性分析
欢迎转载,尊重原创,所以转载请注明出处: http://blog.csdn.net/bendanban/article/details/30527785 本文讲述了OpenCV中几种访问矩阵元素的方法 ...
- 一些日常工具集合(C++代码片段)
一些日常工具集合(C++代码片段) ——工欲善其事,必先利其器 尽管不会松松松,但是至少维持一个比较小的常数还是比较好的 在此之前依然要保证算法的正确性以及代码的可写性 本文依然会持久更新,因为一次写 ...
- OO_Unit2 关于性能优化与测试的那些事
OO_Unit2 关于性能优化与测试的那些事 OO的第2单元到本周也就正式完结了.尽管这个单元的主旋律是多线程,但"面向对象"的基本思想仍然是我们一切架构与优化的出发点与前提.因此 ...
- Web 应用性能和压力测试工具 Gor - 运维生存时间
Web 应用性能和压力测试工具 Gor - 运维生存时间 undefined 无需花生壳,dnspod实现ddns - 推酷 undefined
- Nodejs开源项目里怎么样写测试、CI和代码测试覆盖率
测试 目前主流的就bdd和tdd,自己查一下差异 推荐 mocha和tape 另外Jasmine也挺有名,angularjs用它,不过挺麻烦的,还有一个选择是qunit,最初是为jquery测试写的, ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(三)代码测试
日常啰嗦 看到标题你可能会问为什么这一篇会谈到代码测试,不是说代码优化么?前两篇主要是讲了程序的输出及Log4j的使用,Log能够帮助我们进行bug的定位,优化开发流程,而代码测试有什么用呢?其实测试 ...
- 进行app性能和安全性测试的重要性
如何让用户感觉App运行速度更快呢,这需要对App进行性能测试.限制App性能的因素按照App的系统结构分为App自身和App需要用到的后台服务. 测试App连接网络的速度 一般采用在模拟Mock环境 ...
- HashMap在JDK1.8中并发操作,代码测试以及源码分析
HashMap在JDK1.8中并发操作不会出现死循环,只会出现缺数据.测试如下: package JDKSource; import java.util.HashMap; import java.ut ...
- iOS 覆盖率检测原理与增量代码测试覆盖率工具实现
背景 对苹果开发者而言,由于平台审核周期较长,客户端代码导致的线上问题影响时间往往比较久.如果在开发.测试阶段能够提前暴露问题,就有助于避免线上事故的发生.代码覆盖率检测正是帮助开发.测试同学提前发现 ...
随机推荐
- cygwin--简单备忘
cygwin是windows上使用linux的一个东东 linux中可以apt-get来安装软件,在cygwin中则使用apt-cyg来安装软件 具体怎么玩的呢: 1.下载cygwin 2.下载并且修 ...
- poj 3321
题目链接 题意:一开始1-n都有苹果,Q查询以x为根下存在多少. 树状数组+DFS+队列转换 这题纠结了2天,一开始一点思路都没有,看大神都是吧树状数组转换成队列来做 看了好久都不知道怎么转换的, 解 ...
- UML--核心视图之用例图
如果说UML是一门语言,那么元素就是UML的基本词汇,视图就是语法. UML通过视图将基本元素组织在一起,形成有意义的句子. 静态视图,顾名思义,就是表达静态事物的.包括用例图.类图和包图. 用例图 ...
- c# list exists(contains) delegate 委托判断 元素是否在LIST中存在
static void Main(string[] args) { List<GoodsInfo> list = new List<GoodsIn ...
- HTTP缓存 1.0 vs 1.1
在“使用ETag跟踪用户”中有一点被忽略了,因为要用这张小图统计统计uv, 所以要求浏览器必须每次都要发送这个图片的请求.这需要服务器对图片的缓存策略做设置. http/1.0 和 http/1.1 ...
- 第02讲- Android开发环境
第02讲Android开发环境 需要下载的软件: JDK(JavaDevelopment Kit) Eclipse AndroidSDK(SoftwareDevelopmentKit) ADT(And ...
- hdu 5429 Geometric Progression(存个大数模板)
Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...
- MVVM 介绍
我会修改一个关键架构,并将其带入我从那时起就在开发的各种应用,即使用一种叫做 Model-View-ViewModel 的架构替换 Model-View-Controller. 所以,MVVM 到底是 ...
- (转)mysql分表的3种方法
原文:http://blog.51yip.com/mysql/949.html 一,先说一下为什么要分表 当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿 ...
- Android学习总结——判断网络状态
package com.example.xch.broadcasttest; import android.content.BroadcastReceiver; import android.cont ...