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 覆盖率检测原理与增量代码测试覆盖率工具实现
背景 对苹果开发者而言,由于平台审核周期较长,客户端代码导致的线上问题影响时间往往比较久.如果在开发.测试阶段能够提前暴露问题,就有助于避免线上事故的发生.代码覆盖率检测正是帮助开发.测试同学提前发现 ...
随机推荐
- 常用IC封装技术介绍
1.BGA(ball grid array)球形触点陈列,表面贴装型封装之一.在印刷基板的背面按陈列方式制作出球形凸点用 以 代替引脚,在印刷基板的正面装配LSI 芯片,然后用模压树脂或灌封方法进行密 ...
- tp28xx port pin (open-drain )and (push-pull) 和open collector)
具有开漏(OD)输出的器件是指内部输出和地之间有个N沟道的MOSFET(T1),这些器件可以用于电平转换的应用.输出电压由Vcc'决定.Vcc'可以大于输入高电平电压VCC(up-translate) ...
- Qt编程之UI与控件布局
当然,大家都知道UI界面可以用Qt Designer在约束环境下设置编辑.ui文件,再将.ui文件转换成对应的ui_XXX.h文件,这头文件中的内容是:用C++语言实现真正的界面布局.uic -o & ...
- MS Build参考
以下链接很详细的讲述了Build方面相关的知识,顺带连Link的参数都解释了,以后不知道就来这里查一查了. http://msdn.microsoft.com/zh-CN/library/ee8624 ...
- MySQL加锁分析
参考:MySQL 加锁处理分析.该文已经讲的很详尽了,也易懂,下面仅仅是个人做的总结. 一. 背景 1.1 隔离级别 1.2 加锁过程 逐条处理,逐条加锁. 1.3 两阶段锁2PL 1.4 gap锁 ...
- 【转】android4.1.2 CTS测试总结
原文网址:http://blog.csdn.net/bboyliaoye/article/details/17299259 公司客户的产品要过google GMS认证,首先要过的就是兼容性测试(CTS ...
- Windows下AndroidStudio 中使用Git(AndroidStudio项目于GitHub关联)
前提条件 : 1. 安装 Git 客户端 下载链接 2. 有 GitHub 账号 (假设你已经有了一些git基础, 如果还一点都不会, 请去找其他加成学习) AndroidStudio项目发布到Git ...
- Linux 块设备驱动 (二)
linux下Ramdisk驱动 1 什么是Ramdisk Ramdisk是一种模拟磁盘,其数据实际上是存储在RAM中,它使用一部分内存空间来模拟出一个磁盘设备,并以块设备的方式来组织和访问这片内存.对 ...
- java集合总结【转】
Map.Set.Iterator迭代详解 Map接口定义了四种类型的方法,每个Map都包含这些方法. equals(Object o)比较指定对象与此Map的等价性. hashCode()返回此Map ...
- [Regular Expressions] Find the Start and End of Whole Words
Regular Expression Word Boundaries allow to perform "whole word only" searches within our ...