您的位置:首页 > 编程语言 > C#

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

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

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

测试结果

查询方式/单位(ms)Debug模式Release模式
for4.21.6
foreach73.2
FirstOrDefault119
Find4.41.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<model> list=new List<model>();


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<model> list;
model p1,p2;
/// <summary>
/// for的方式查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}

/// <summary>
/// foreach的方式查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}

/// <summary>
/// Find的方式查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}

/// <summary>
/// FirstOrDefault的方式查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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<model>();
list.Add(p1);
list.Add(p2);
}

}
public class model
{
public string name { set; get; }
public int age { set; get; }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  测试 List 效率优化 c#