zoukankan      html  css  js  c++  java
  • 解析Hashtable、Dictionary、SortedDictionary、SortedList的效率

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;

    namespace DictionaryTest
    {
    class Program
    {
    private static int totalCount = 10000;

    static void Main(string[] args)
    {
    HashtableTest();
    DictionaryTest();
    SortedDictionaryTest();
    Console.ReadKey();
    }

    private static void HashtableTest()
    {
    Hashtable hastable
    = new Hashtable();
    Stopwatch watch
    = new Stopwatch();
    watch.Start();
    for (int i = 1; i < totalCount; i++)
    {
    hastable.Add(i,
    0);
    }
    watch.Stop();
    Console.WriteLine(
    string.Format("Hashtable添加{0}个元素耗时:{1}ms", totalCount, watch.ElapsedMilliseconds));
    Console.WriteLine(
    "Hashtable不做查找测试");
    hastable.Clear();
    }

    private static void DictionaryTest()
    {
    Dictionary
    <int, int> dictionary = new Dictionary<int, int>();
    Stopwatch watch
    = new Stopwatch();
    watch.Start();
    for (int i = 1; i < totalCount; i++)
    {
    dictionary.Add(i,
    0);
    }
    watch.Stop();
    Console.WriteLine(
    string.Format("Dictionary添加{0}个元素耗时:{1}ms", totalCount, watch.ElapsedMilliseconds));
    watch.Reset();
    watch.Start();
    dictionary.Select(o
    => o.Key % 1000 == 0).ToList().ForEach(o => { });
    watch.Stop();
    Console.WriteLine(
    string.Format("Dictionary查找能被1000整除的元素耗时:{0}ms", watch.ElapsedMilliseconds));
    dictionary.Clear();
    }

    private static void SortedDictionaryTest()
    {
    SortedDictionary
    <int, int> dictionary = new SortedDictionary<int, int>();
    Stopwatch watch
    = new Stopwatch();
    watch.Start();
    for (int i = 1; i < totalCount; i++)
    {
    dictionary.Add(i,
    0);
    }
    watch.Stop();
    Console.WriteLine(
    string.Format("SortedDictionary添加{0}个元素耗时:{1}ms", totalCount, watch.ElapsedMilliseconds));
    watch.Reset();
    watch.Start();
    dictionary.Select(o
    => o.Key % 1000 == 0).ToList().ForEach(o => { });
    watch.Stop();
    Console.WriteLine(
    string.Format("SortedDictionary查找能被1000整除的元素耗时:{0}ms", watch.ElapsedMilliseconds));
    dictionary.Clear();
    }
    }
    }
    全文:http://www.cnblogs.com/moozi/archive/2010/05/23/1741980.html
  • 相关阅读:
    xcode5 ios6程序图标去掉阴影
    ios程序启动时去掉状态栏
    ios启动图标程序适配
    c语言基础:各种数据类型的输出占位符
    c语言变量作用域问题
    JS 学习笔记--11---内置对象(Global/Math)
    JS 学习笔记--10---基本包装类型
    JS 学习笔记--9---变量-作用域-内存相关
    JS 学习笔记--8---Function类型
    JS 学习笔记--6---日期和时间
  • 原文地址:https://www.cnblogs.com/shikyoh/p/2055196.html
Copyright © 2011-2022 走看看