c#List四种查询方式的效率对比

对List进行查询的时候,会有多种方法供我们使用,但是在追求代码简洁和效率的情况下,哪种方法才是我们的最优选择呢?


先把我的测试结果摆出了吧~

测试结果

查询方式/单位(ms) Debug模式 Release模式
for 4.2 1.6
foreach 7 3.2
FirstOrDefault 11 9
Find 4.4 1.8

for的效率是最高的,但是再兼顾行数和效率的情况下首选Find


测试内容

1.计算程序的运行时间

    //用Stopwatch类(System.Diagnostics)计算耗时
    static void SubTest()  
    {  
        Stopwatch sw = new Stopwatch();  
        sw.Start();  

        //耗时巨大的代码  

        sw.Stop();  
        TimeSpan ts2 = sw.Elapsed;  
        Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);  
    }  

2.测试List

 public class model
 {
      public string name { set; get; }
      public int age { set; get; }
 }
 List list=new List();

List查询方法

  • for
int myage ;
for (int i = 0; i < list.Count; i++)
{
    if (list[i].name == "小明") myage = list[i].age;
}
  • foreach
int myage ;
foreach(model m in list)
{
     if(m.name=="小明") myage = m.age;
}
  • Find
int myage = list.Find(x => x.name == "小明").age;
  • FirstOrDefault
int myage = list.FirstOrDefault(s => s.name == "小明").age;

完整代码

public partial class Form1 : Form
    {
        List list;
        model p1,p2;
        /// 
        /// for的方式查询
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
            int count = 0;
            int myage;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            while (count < 100000)//计算循环100000次的耗时
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].name == "小明")myage = list[i].age;
                }
                count++;
            }
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);
        }

        /// 
        /// foreach的方式查询
        /// 
        /// 
        /// 
        private void button2_Click(object sender, EventArgs e)
        {
            int count = 0;
            int myage;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            while (count < 100000)
            {
                foreach(model m in list)
                {
                    if(m.name=="小明") myage = m.age;
                }
                count++;
            }                      
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);
        }

        /// 
        /// Find的方式查询
        /// 
        /// 
        /// 
        private void button3_Click(object sender, EventArgs e)
        {
            int count = 0;
            int myint;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            while (count < 100000)
            {
                myint = list.Find(x => x.name == "小明").age;
                count++;
            }
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);
        }

        /// 
        /// FirstOrDefault的方式查询
        /// 
        /// 
        /// 
        private void button4_Click(object sender, EventArgs e)
        {
            int count=0;
            int myage;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            while (count < 100000)
            {
                myage = list.FirstOrDefault(s => s.name == "小明").age;
                count++;
            }
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            p1 = new model();
            p1.name = "小红";
            p1.age = 10;
            p2 = new model();
            p2.name = "小明";
            p2.age = 23;
            list = new List();
            list.Add(p1);
            list.Add(p2);
        }

    }
    public class model
    {
        public string name { set; get; }
        public int age { set; get; }
    }

你可能感兴趣的:(c#)