Interesting difference (operators vs expressions)
-
txtbox_result.Text = "";
DateTime start = DateTime.Now; var processes = System.Diagnostics.Process.GetProcesses() .Where(process => process.WorkingSet64 > 20\*1024\*1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name=process.ProcessName }); foreach(var p in processes){ txtbox\_result.Text += "Id=" + p.Id + "\\tProcess Name=" + p.Name + Environment.NewLine; } DateTime end = DateTime.Now; txtbox\_result.Text += Convert.ToString((end - start).TotalMilliseconds) + " milliseconds" + Environment.NewLine; txtbox\_result.Text += "Processes: " + Convert.ToString(System.Diagnostics.Process.GetProcesses().Length) + Environment.NewLine + Environment.NewLine; start = DateTime.Now; var processes2 = from process in System.Diagnostics.Process.GetProcesses() where process.WorkingSet64 > 20\*1024\*1024 orderby process.WorkingSet64 descending select new { process.Id, Name=process.ProcessName }; foreach (var p2 in processes2) { txtbox\_result.Text += "Id=" + p2.Id + "\\tProcess Name=" + p2.Name + Environment.NewLine; } end = DateTime.Now; txtbox\_result.Text += Convert.ToString((end - start).TotalMilliseconds) + " milliseconds" + Environment.NewLine; txtbox\_result.Text += "Processes: " + Convert.ToString(System.Diagnostics.Process.GetProcesses().Length) + Environment.NewLine + Environment.NewLine;
produces
Id=3700 Process Name=devenv
Id=6136 Process Name=devenv
Id=388 Process Name=svchost
Id=6016 Process Name=OUTLOOK
Id=3392 Process Name=ReportingServicesService
Id=972 Process Name=MsMpEng
Id=2940 Process Name=sqlservr
Id=5024 Process Name=iexplore
Id=2996 Process Name=msnmsgr
Id=2452 Process Name=explorer
Id=432 Process Name=svchost
Id=3056 Process Name=sidebar
Id=6788 Process Name=iexplore
Id=2084 Process Name=communicator
Id=3276 Process Name=msmdsrv
Id=2388 Process Name=dwm
Id=2364 Process Name=iexplore
Id=8776 Process Name=LinqTest.vshost
Id=5240 Process Name=iexplore
Id=1388 Process Name=svchost
Id=2836 Process Name=explorer
Id=4852 Process Name=SearchIndexer
Id=2500 Process Name=fdm
Id=5888 Process Name=wlcomm
Id=1860 Process Name=MsDtsSrvr
Id=2088 Process Name=iexplore
Id=5216 Process Name=iexplore
Id=136 Process Name=svchost
Id=1652 Process Name=svchost
16,0016 milliseconds
Processes: 80Id=3700 Process Name=devenv
Id=6136 Process Name=devenv
Id=388 Process Name=svchost
Id=6 -
txtbox_result.Text = "";
DateTime start = DateTime.Now; var processes = System.Diagnostics.Process.GetProcesses() .Where(process => process.WorkingSet64 > 20\*1024\*1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name=process.ProcessName }); foreach(var p in processes){ txtbox\_result.Text += "Id=" + p.Id + "\\tProcess Name=" + p.Name + Environment.NewLine; } DateTime end = DateTime.Now; txtbox\_result.Text += Convert.ToString((end - start).TotalMilliseconds) + " milliseconds" + Environment.NewLine; txtbox\_result.Text += "Processes: " + Convert.ToString(System.Diagnostics.Process.GetProcesses().Length) + Environment.NewLine + Environment.NewLine; start = DateTime.Now; var processes2 = from process in System.Diagnostics.Process.GetProcesses() where process.WorkingSet64 > 20\*1024\*1024 orderby process.WorkingSet64 descending select new { process.Id, Name=process.ProcessName }; foreach (var p2 in processes2) { txtbox\_result.Text += "Id=" + p2.Id + "\\tProcess Name=" + p2.Name + Environment.NewLine; } end = DateTime.Now; txtbox\_result.Text += Convert.ToString((end - start).TotalMilliseconds) + " milliseconds" + Environment.NewLine; txtbox\_result.Text += "Processes: " + Convert.ToString(System.Diagnostics.Process.GetProcesses().Length) + Environment.NewLine + Environment.NewLine;
produces
Id=3700 Process Name=devenv
Id=6136 Process Name=devenv
Id=388 Process Name=svchost
Id=6016 Process Name=OUTLOOK
Id=3392 Process Name=ReportingServicesService
Id=972 Process Name=MsMpEng
Id=2940 Process Name=sqlservr
Id=5024 Process Name=iexplore
Id=2996 Process Name=msnmsgr
Id=2452 Process Name=explorer
Id=432 Process Name=svchost
Id=3056 Process Name=sidebar
Id=6788 Process Name=iexplore
Id=2084 Process Name=communicator
Id=3276 Process Name=msmdsrv
Id=2388 Process Name=dwm
Id=2364 Process Name=iexplore
Id=8776 Process Name=LinqTest.vshost
Id=5240 Process Name=iexplore
Id=1388 Process Name=svchost
Id=2836 Process Name=explorer
Id=4852 Process Name=SearchIndexer
Id=2500 Process Name=fdm
Id=5888 Process Name=wlcomm
Id=1860 Process Name=MsDtsSrvr
Id=2088 Process Name=iexplore
Id=5216 Process Name=iexplore
Id=136 Process Name=svchost
Id=1652 Process Name=svchost
16,0016 milliseconds
Processes: 80Id=3700 Process Name=devenv
Id=6136 Process Name=devenv
Id=388 Process Name=svchost
Id=6Use ObjectQuery.ToTraceString()[^] to review the queries that are being produced to compare them Also, rather than using DataTime you can use System.Diagnostics.Stopwatch[^] for the purpose of timing.
I know the language. I've read a book. - _Madmatt
-
txtbox_result.Text = "";
DateTime start = DateTime.Now; var processes = System.Diagnostics.Process.GetProcesses() .Where(process => process.WorkingSet64 > 20\*1024\*1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name=process.ProcessName }); foreach(var p in processes){ txtbox\_result.Text += "Id=" + p.Id + "\\tProcess Name=" + p.Name + Environment.NewLine; } DateTime end = DateTime.Now; txtbox\_result.Text += Convert.ToString((end - start).TotalMilliseconds) + " milliseconds" + Environment.NewLine; txtbox\_result.Text += "Processes: " + Convert.ToString(System.Diagnostics.Process.GetProcesses().Length) + Environment.NewLine + Environment.NewLine; start = DateTime.Now; var processes2 = from process in System.Diagnostics.Process.GetProcesses() where process.WorkingSet64 > 20\*1024\*1024 orderby process.WorkingSet64 descending select new { process.Id, Name=process.ProcessName }; foreach (var p2 in processes2) { txtbox\_result.Text += "Id=" + p2.Id + "\\tProcess Name=" + p2.Name + Environment.NewLine; } end = DateTime.Now; txtbox\_result.Text += Convert.ToString((end - start).TotalMilliseconds) + " milliseconds" + Environment.NewLine; txtbox\_result.Text += "Processes: " + Convert.ToString(System.Diagnostics.Process.GetProcesses().Length) + Environment.NewLine + Environment.NewLine;
produces
Id=3700 Process Name=devenv
Id=6136 Process Name=devenv
Id=388 Process Name=svchost
Id=6016 Process Name=OUTLOOK
Id=3392 Process Name=ReportingServicesService
Id=972 Process Name=MsMpEng
Id=2940 Process Name=sqlservr
Id=5024 Process Name=iexplore
Id=2996 Process Name=msnmsgr
Id=2452 Process Name=explorer
Id=432 Process Name=svchost
Id=3056 Process Name=sidebar
Id=6788 Process Name=iexplore
Id=2084 Process Name=communicator
Id=3276 Process Name=msmdsrv
Id=2388 Process Name=dwm
Id=2364 Process Name=iexplore
Id=8776 Process Name=LinqTest.vshost
Id=5240 Process Name=iexplore
Id=1388 Process Name=svchost
Id=2836 Process Name=explorer
Id=4852 Process Name=SearchIndexer
Id=2500 Process Name=fdm
Id=5888 Process Name=wlcomm
Id=1860 Process Name=MsDtsSrvr
Id=2088 Process Name=iexplore
Id=5216 Process Name=iexplore
Id=136 Process Name=svchost
Id=1652 Process Name=svchost
16,0016 milliseconds
Processes: 80Id=3700 Process Name=devenv
Id=6136 Process Name=devenv
Id=388 Process Name=svchost
Id=6You're not measuring how long the LINQ query takes... You're measuring how long it's taking to print the results into a text box on your GUI, which is MUCH slower. Compared to that, enumerating the processes takes no time at all. The difference comes from the string concatenations. Each time you print a line, you're creating a new string, copying the existing output into that string, copying the new output to the end of it, and setting the contents of the text box to that new value. That sequence of operations takes longer when dealing with longer strings, so it gets slower and slower as you add more and more to the text box. Try swapping the two expressions... I'd wager you'll still see more time elapsed for whichever one you do second.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
You're not measuring how long the LINQ query takes... You're measuring how long it's taking to print the results into a text box on your GUI, which is MUCH slower. Compared to that, enumerating the processes takes no time at all. The difference comes from the string concatenations. Each time you print a line, you're creating a new string, copying the existing output into that string, copying the new output to the end of it, and setting the contents of the text box to that new value. That sequence of operations takes longer when dealing with longer strings, so it gets slower and slower as you add more and more to the text box. Try swapping the two expressions... I'd wager you'll still see more time elapsed for whichever one you do second.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)