如何查看自制词典的执行效率

本文介绍如何使用C#创建一个字典,并通过添加、查找等操作实现高效的数据管理。通过读取TXT文件内容,将英文词汇与解释进行对应存储,展示了在实际应用中如何利用C#的集合类简化复杂任务。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace _EditDictionary
{
    class KeyValuePair
    {
        public KeyValuePair()
        {
        
        }

        public KeyValuePair(string key,string value)
        {
            this.key = key;
            this.value = value;
        }
        private string key;
        public string Key
        {
             get { return key; }
             set { key = value; }
        }
    
        private string value;
        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }


    }

    class Mydic
    {
        List<KeyValuePair> list = new List<KeyValuePair>();

        public void Add(string key, string value)
        {
            list.Add(new KeyValuePair(key,value));
        }

        public bool ContainKey(string key)
        { 
            bool result=false;
            foreach(KeyValuePair kvp in list)
            {
                if (kvp.Key == key)
                {
                    result = true;
                  //  value += kvp.Value ;                  
                    break;
                }
            }
            return result;        
        }
        
    }

    class Program
    {
        static void Main(string[] args)
        {
            
          
            Mydic dic = new Mydic();
            string[] arr = File.ReadAllLines(@"D:\英汉词典TXT格式.txt", Encoding.Default);//第二个参数是防止乱码;第一个参数是字典的txt文档(找一个大的txt字典,测试程序才能更有效)

            foreach(string item in arr)
            {
                //分割出单词和解释
                string[] strArr = item.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);

                if (strArr.Length == 2)
                { 
                    if(!dic.ContainKey(strArr[0]))
                    {
                        dic.Add(strArr[0],strArr[1]);
                    }
                }
            }

            Stopwatch sw = new Stopwatch();
            sw.Start();
            dic.ContainKey("china");
            sw.Stop();
            Console.WriteLine(sw.Elapsed);
            Console.WriteLine();
            Console.Read();

        }
    }
}

耗时: 

 

转载于:https://www.cnblogs.com/5696-an/p/4995687.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值