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 覆盖率检测原理与增量代码测试覆盖率工具实现
背景 对苹果开发者而言,由于平台审核周期较长,客户端代码导致的线上问题影响时间往往比较久.如果在开发.测试阶段能够提前暴露问题,就有助于避免线上事故的发生.代码覆盖率检测正是帮助开发.测试同学提前发现 ...
随机推荐
- BZOJ 2521 最小生成树(最小割)
http://www.lydsy.com/JudgeOnline/problem.php?id=2521 题意:每次能增加一条边的权值1,求最小代价让一条边保证在最小生成树里 思路:如果两个点中有环, ...
- LeetCode_Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given -> ...
- Win7/Win8/Win8.1众多版本,我该选择哪个?
当你要下载Win7或者Win8/8.1镜像时,是不是被Windows版本种类给吓着了?到底该选择哪种版本的?其实,大多数人用的就那一两个版本,这也是推荐选择的版本,请看快速通道.如果你想了解的更多一点 ...
- keil c51编译器的一些使用心得
现在的存储器已经不像七八年前那样昂贵了,但是ram相对于rom和eeprom的价格还是不可同样看待的,所以程序中节省内存在现在看来还是非常关键的.原因有以下几点: 1.ram的存取速度相对于eepro ...
- wapPush
短信推送:wapPsuh简介
- 5.1.1 读取Redis 数据
Redis 服务器是Logstash 推荐的Broker选择,Broker 角色就意味会同时存在输入和输出两个插件. 5.1.1 读取Redis 数据 LogStash::Input::Redis 支 ...
- logstash 内置匹配IP
10.252.142.174 - - [06/Sep/2016:08:41:36 +0800] "GET /api/validate/code/send?mobilePhone=186522 ...
- jquery使用总结
jquery使用总结-常用DOM操作 (1)查询或设置元素属性操作 html() //获取匹配元素集合中的第1个元素 html(htmlString) //为匹配集合中的所有元素设置内容 tex ...
- QT文件夹定位(网友提供)
#ifndef FOLDERFINDER_H #define FOLDERFINDER_H#include <QDir>class FolderFinder{public: QStr ...
- 多线程下不反复读取SQL Server 表的数据
在进行一些如发送短信.邮件的业务时,我们常常会使用一个表来存储待发送的数据,由后台多个线程不断的从表中读取待发送的数据进行发送.发送完毕后再将数据转移到历史表中,这样保证待发送表的数据普通情况下不会太 ...