码迷,mamicode.com
首页 > 其他好文 > 详细

C# 集合性能比较(代码测试)

时间:2014-08-14 01:10:07      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   ar   

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 = 100000;
            int[] x = new int[9999999];
            for (int i = 0; i < x.Length; i++)
            {
                x[i] = i;
            }
            int[] x2 = new int[] { 6666, 333, 11111, 56764, 33335 };
            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();
        }
    }
}

bubuko.com,布布扣

C# 集合性能比较(代码测试),布布扣,bubuko.com

C# 集合性能比较(代码测试)

标签:style   blog   http   color   os   io   for   ar   

原文地址:http://www.cnblogs.com/mingxuantongxue/p/3911316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!