Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Interesting difference (operators vs expressions)

Interesting difference (operators vs expressions)

Scheduled Pinned Locked Moved LINQ
visual-studioworkspace
4 Posts 3 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    V 0
    wrote on last edited by
    #1

    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: 80

    Id=3700 Process Name=devenv
    Id=6136 Process Name=devenv
    Id=388 Process Name=svchost
    Id=6

    N I 2 Replies Last reply
    0
    • V V 0

      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: 80

      Id=3700 Process Name=devenv
      Id=6136 Process Name=devenv
      Id=388 Process Name=svchost
      Id=6

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Use 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

      1 Reply Last reply
      0
      • V V 0

        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: 80

        Id=3700 Process Name=devenv
        Id=6136 Process Name=devenv
        Id=388 Process Name=svchost
        Id=6

        I Offline
        I Offline
        Ian Shlasko
        wrote on last edited by
        #3

        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)

        V 1 Reply Last reply
        0
        • I Ian Shlasko

          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)

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #4

          It does switch :-), good point. Should have known really.

          V.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups