c#List四種查詢方式的效率對比
阿新 • • 發佈:2019-01-23
對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<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; }
}