List和HashTable的测试

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

namespace ListHashTableTest
{
    
class Program
    {
        
static void Main(string[] args)
        {
            User myUser;
            
            DateTime dt1;
            DateTime dt2;
            dt1 
= DateTime.Now;
            List
<User> list = new List<User>();
            
for (int i = 0; i < 50000;i++ )
            {
                myUser 
= new User(i.ToString(), i.ToString());

                list.Add(myUser);
            }
            dt2 
= DateTime.Now;
            Console.WriteLine(
"List添加10000项数据用时:"+dt2.Subtract(dt1).ToString());
            

            dt1 
= DateTime.Now;
            Hashtable ht 
= new Hashtable();
            
for (int i = 0; i < 50000;i++ )
            {
                myUser 
= new User(i.ToString(), i.ToString());
                ht.Add(i.ToString(), myUser);
            }
            dt2 
= DateTime.Now;
            Console.WriteLine(
"HashTable添加10000项数据用时:" + dt2.Subtract(dt1).ToString());

            
            
string strHt=string.Empty;
            
string strList = string.Empty;

            dt1 
= DateTime.Now;
            
foreach (User user in list)
            {
                strList 
+= user.Name;
            }
            dt2 
= DateTime.Now;
            Console.WriteLine(
"List遍历10000项数据用时:" + dt2.Subtract(dt1).ToString());

            dt1 
= DateTime.Now;
            
foreach (User user in ht.Values)
            {
                strHt 
+= user.Name;
            }
            dt2 
= DateTime.Now;
            Console.WriteLine(
"HashTable遍历100000项数据用时:" + dt2.Subtract(dt1).ToString());

            dt1 
= DateTime.Now;

            dt2 
= DateTime.Now;
            Console.WriteLine(
"List查找一项项数据用时:" + dt2.Subtract(dt1).ToString());


            dt1 
= DateTime.Now;
            
string aaa = ((User)(ht["8907"])).Name;
            dt2 
= DateTime.Now;
            Console.WriteLine(
"HashTable查找一项数据用时:" + dt2.Subtract(dt1).ToString());


            Console.Read();
        }
    }

    
class User
    {
        
public string Name { get;set;}
        
public string Role {get;set;}
        
public User(string name,string role)
        {
            Name 
= name;
            Role 
= role;
        }
     
    }
}
posted @ 2009-09-27 14:35  翁玉礼  阅读(773)  评论(0编辑  收藏  举报