which one is better for filling DataGridView lambda or linq?
i filled 2 dataGridView with two kinds of method:
1) Lambda Expression:
protected void FillLamdaMethod()
{
Stopwatch sw = Stopwatch.StartNew();
using (eCommerceContext ctx = new eCommerceContext())
{
List<table_bilgisayar> listBilgisayar = new List<table_bilgisayar>();
listBilgisayar = ctx.table_bilgisayar.ToList();
dataGridViewLamda.DataSource = listBilgisayar;//qry.AsEnumerable();
}
sw.Stop();
lblLamdaResult.Text = String.Format("Time used (float): {0} ms",sw.Elapsed.TotalMilliseconds)+Environment.NewLine;
lblLamdaResult.Text+=String.Format("Time used (rounded): {0} ms", sw.ElapsedMilliseconds);
}
2) Linq Method:
protected void FillClassicMethod()
{
Stopwatch sw = Stopwatch.StartNew();
using (eCommerceContext ctx = new eCommerceContext())
{
List<table_bilgisayar> listBilgisayar = new List<table_bilgisayar>();
listBilgisayar =(from q in ctx.table_bilgisayar select q).ToList();
dataGridViewClasicLinq.DataSource = listBilgisayar;//(from q in ctx.table_bilgisayar select q.model).ToList();
}开发者_开发问答
sw.Stop();
lblClassicResult.Text = String.Format("Time used (float): {0} ms", sw.Elapsed.TotalMilliseconds)+Environment.NewLine;
lblClassicResult.Text += String.Format("Time used (rounded): {0} ms", sw.ElapsedMilliseconds);
}
i have 2 important question
1) this stopwatch method is correct or sufficient or is there ant better method to compute performance? 2) Up this time; i know that lambda expression is MORE FASTER than classic linq ( from x in table etc...) But Test result is surprising: 1) Lambda Method : 867 ms 2) Linq Method: 39 ms This result is correct? i expect it must be just the opposite...ALSO click fillButton to call this methods. performance result stupidly change. i think that this is crazy. 867 ms second click result 56 ms third click 45 ms....
I think to get any kind of reliable result you would have to modify your test slightly.
Maybe you could start the Stopwatch, loop a 1000 times calling your fill method, stop the Stopwatch and then simply divide your result by 1000 to get an average.
Results can change drastically for a number of reasons (e.g other processes and/or threads eating up CPU or the .NET Garbage Collection running). Peforming a test multiple times and taking an average will help smoothe out any discrepancies.
I am surprised there is any difference, i thought it's just a different syntax. Maybe your Select (q=>q)
slows it down. Try
listBilgisayar = ctx.table_bilgisayar.ToList();
精彩评论