{"id":516,"date":"2013-08-27T12:40:48","date_gmt":"2013-08-27T12:40:48","guid":{"rendered":"http:\/\/ntspl.co.in\/blog\/?p=516"},"modified":"2021-12-20T11:03:23","modified_gmt":"2021-12-20T11:03:23","slug":"parallel-linq-vs-simple-linq-in-asp-net","status":"publish","type":"post","link":"https:\/\/www.ntspl.co.in\/blog\/parallel-linq-vs-simple-linq-in-asp-net\/","title":{"rendered":"Parallel LINQ Vs Simple LINQ in asp.net"},"content":{"rendered":"<p><strong>Language-Integrated Query (LINQ)<\/strong> was introduced in the .NET Framework 3.5. It features a unified model for querying any System.Collections.IEnumerable or System.Collections.Generic.IEnumerable&lt;T&gt; data source in a type-safe manner. LINQ to Objects is the name for LINQ queries that are run against in-memory collections such as List&lt;T&gt; and arrays. This article assumes that you have a basic understand of LINQ.<\/p>\n<p><strong>Parallel LINQ (PLINQ)<\/strong> is a parallel implementation of the LINQ pattern. A PLINQ query in many ways resembles a non-parallel LINQ to Objects query. PLINQ queries, just like sequential LINQ queries, operate on any in-memory IEnumerable or IEnumerable&lt;T&gt; data source, and have deferred execution, which means they do not begin executing until the query is enumerated. The primary difference is that PLINQ attempts to make full use of all the processors on the system. It does this by partitioning the data source into segments, and then executing the query on each segment on separate worker threads in parallel on multiple processors. In many cases, parallel execution means that the query runs significantly faster.<\/p>\n<p><em><span style=\"text-decoration: underline;\"><strong>Demo:<\/strong><\/span><\/em><\/p>\n<p>Step 1: Create a asp.net page in 4.0 version<\/p>\n<p>Step 2 : write the code in .cs fie<\/p>\n<pre class=\"code-structure\"><code>\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Web;\r\nusing System.Web.UI;\r\nusing System.Web.UI.WebControls;\r\n<strong>using System.Diagnostics;<\/strong>\r\n\r\npublic partial class TestPLinq : System.Web.UI.Page {\r\n\tprotected void Page_Load(object sender, EventArgs e) {\r\n\t\t<strong>if (!IsPostBack) {<\/strong>\r\n\t\t\t<strong> IEnumerable&lt;int&gt; rng = Enumerable.Range(1, 10000000);<\/strong>\r\n\t\t\t<strong> var query = rng.Where(d =&gt; d % 1234567 == 0).Select(d =&gt; d);<\/strong>\r\n\t\t\t<strong> Stopwatch sw = Stopwatch.StartNew();<\/strong>\r\n\t\t\t<strong> foreach (var v in query) {<\/strong>\r\n\t\t\t\t<strong> Response.Write(v+\"&lt;br\/&gt;\");<\/strong>\r\n\t\t\t<strong> }<\/strong>\r\n\t\t\t<strong> Response.Write(\"Time in Normal LINq time: \"+sw.ElapsedMilliseconds+\" ms\");<\/strong>\r\n\t\t\t<strong class=\"comment\"> \/\/\/\/Parallel<\/strong>\r\n\t\t\t<strong> Response.Write(\"&lt;br\/&gt;\");<\/strong>\r\n\t\t\t<strong> sw.Restart();<\/strong>\r\n\r\n\t\t\t<strong>var query1 = rng.AsParallel().Where(d =&gt; d % 1234567 == 0).Select(d =&gt; d);<\/strong>\r\n\t\t\t<strong> foreach (var v in query) {<\/strong>\r\n\t\t\t\t<strong> Response.Write(v + \"&lt;br\/&gt;\");<\/strong>\r\n\t\t\t<strong> }<\/strong>\r\n\t\t\t<strong> Response.Write(\"Time in Parallel time: \" + sw.ElapsedMilliseconds + \" ms\");<\/strong>\r\n\t\t<strong> }<\/strong>\r\n\t<strong> }<\/strong>\r\n}\r\n<\/code><\/pre>\n<p><em><span style=\"text-decoration: underline;\"><strong>Result:<\/strong><\/span><\/em><br \/>\n<output>1234567<br \/>\n2469134<br \/>\n3703701<br \/>\n4938268<br \/>\n6172835<br \/>\n7407402<br \/>\n8641969<br \/>\n9876536<br \/>\n<strong>Time in normal LINQ query: 539 ms<\/strong><br \/>\n<\/output><br \/>\n<output>1234567<br \/>\n2469134<br \/>\n3703701<br \/>\n4938268<br \/>\n6172835<br \/>\n7407402<br \/>\n8641969<br \/>\n9876536<br \/>\n<strong>Time in Parallel query: 521 ms<\/strong><br \/>\n<\/output><\/p>\n<p>For print 8 number different 18 sec<\/p>\n<p>Happy Coding&#8230;.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Language-Integrated Query (LINQ) was introduced in the .NET Framework 3.5. It features a unified model for querying any System.Collections.IEnumerable or System.Collections.Generic.IEnumerable&lt;T&gt; data source in a type-safe manner. LINQ to Objects is the name for LINQ queries that are run against in-memory collections such as List&lt;T&gt; and arrays. This article assumes that you have a basic [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":1493,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[213,267],"class_list":["post-516","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-dot-net","tag-linq","tag-parallel-linq"],"acf":{"custom_meta_title":"Difference Between Parallel LINQ & Simple LINQ | NTSPL","meta_description":"Do you know what a Language-Integrated Query (LINQ) is? Get to know the difference between Simple LINQ and Parallel LINQ. Complete Guide for LINQ.","meta_keyword":"language-integrated query, parallel linq, simple linq","other_meta_tag":"<meta property=og:locale content=\"en-IN\" \/>\r\n<meta property=og:type content=\"website\" \/>\r\n<meta property=og:title content=\"Difference Between Parallel LINQ & Simple LINQ | NTSPL\"\/>\r\n<meta property=og:description content=\"Do you know what a Language-Integrated Query (LINQ) is? Get to know the difference between Simple LINQ and Parallel LINQ. Complete Guide for LINQ.\"\/>\r\n<meta property=og:url content=\"https:\/\/www.ntspl.co.in\/blog\/parallel-linq-vs-simple-linq-in-asp-net\"\/>\r\n<meta property=og:site_name content=NTSPL \/>\r\n<meta name=\"twitter:site\" content=\"@NTSPL\">\r\n<meta name=twitter:card content=\"summary\" \/>\r\n<meta name=twitter:description content=\"Do you know what a Language-Integrated Query (LINQ) is? Get to know the difference between Simple LINQ and Parallel LINQ. Complete Guide for LINQ.\"\/>\r\n<meta name=twitter:title content=\"Difference Between Parallel LINQ & Simple LINQ | NTSPL\"\/>"},"_links":{"self":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts\/516"}],"collection":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/comments?post=516"}],"version-history":[{"count":7,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts\/516\/revisions"}],"predecessor-version":[{"id":4363,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts\/516\/revisions\/4363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/media\/1493"}],"wp:attachment":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/media?parent=516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/categories?post=516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/tags?post=516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}