zoukankan      html  css  js  c++  java
  • GetEnumerator用法

    GetEnumerator是返回实例的枚举数。换句话说就是返回集的中所有元素一个一个列出来。我们可以通过MoveNext()得到集合中的所有元素。

    这里的名词稍微说明一下,

        枚举就:是一个一个的列举。

        枚举数:是循环访问其关联集合的对象。它提供一种方法帮助你遍历一个集合。

      

    GetEnumerator用法请看下边代码中红色部分。 这里顺便作了测试。发现GetEnumerator要比Foreach稍微快一点。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.Diagnostics;
    using System.Collections;

    namespace GetEnumeratorTest
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    const int times =1000;
                Stopwatch watch 
    = new Stopwatch();
                Hashtable hastable 
    = new Hashtable();
                
    for (int i = 0; i < 10000; i++)
                {
                    hastable.Add(i, i.ToString() 
    + "");
                }
                
    //测试GetEnumerator
                watch.Start();
                
    for (int i = 0; i < times; i++)
                {
                    
    IDictionaryEnumerator enumerator = hastable.GetEnumerator();
                    
    while (enumerator.MoveNext())
                    {
                        
    string key = enumerator.Key.ToString();
                        
    string value = enumerator.Value.ToString();
                    }
                }
                watch.Stop();
                Console.WriteLine(
    "Hashtable GetEnumerator耗时" + watch.ElapsedMilliseconds);
                Console.WriteLine(
    "---------------");

                watch.Reset();
                
    //测试ForEach
                watch.Start();
                
    for (int i = 0; i < times; i++)
                {
                    
    foreach (object item in hastable.Keys)
                    {
                        
    string key = item.ToString();
                        
    string value = hastable[item].ToString();
                    }
                }
                watch.Stop();
                Console.WriteLine(
    "Hashtable ForEach耗时" + watch.ElapsedMilliseconds);
                Console.WriteLine(
    "---------------");

                watch.Reset();
                Dictionary
    <intstring> dictionary = new Dictionary<intstring>();
                
    for (int i = 0; i < 10000; i++)
                {
                    dictionary.Add(i, i.ToString() 
    + "");
                }
                watch.Start();
                
    for (int i = 0; i < times; i++)
                {                
                    
    Dictionary<int,string>.Enumerator enumerator = dictionary.GetEnumerator();
                    
    while (enumerator.MoveNext())
                    {
                        
    int key = enumerator.Current.Key;
                        
    string value = enumerator.Current.Value;
                    }
                }
                watch.Stop();
                Console.WriteLine(
    "Dictionary GetEnumerator耗时" + watch.ElapsedMilliseconds);
                Console.WriteLine(
    "---------------");

                watch.Reset();
                
    //测试ForEach
                watch.Start();
                
    for (int i = 0; i < times; i++)
                {
                    
    foreach (int item in dictionary.Keys)
                    {
                        
    int key = item;
                        
    string value = dictionary[item];
                    }
                }
                watch.Stop();
                Console.WriteLine(
    "Dictionary ForEach耗时" + watch.ElapsedMilliseconds);
                Console.WriteLine(
    "---------------");
                Console.ReadKey();
            }
        }
    }

     结果:

        Hashtable GetEnumerator耗时 1584
        Hashtable ForEach耗时 1884
        Dictionary GetEnumerator耗时 365
        Dictionary ForEach耗时 375

  • 相关阅读:
    ACM 人见人爱A^B
    ACM Max Factor
    ACM Primes
    ACM Least Common Multiple
    ACM 最小公倍数
    ACM Bone Collector
    ACM 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
    ACM Piggy Bank
    ACM 饭卡
    ACM Where is the Marble?
  • 原文地址:https://www.cnblogs.com/scottckt/p/2048243.html
Copyright © 2011-2022 走看看