PLINQ (Parallel LINQ)

  Total Views : 138
  Comments : 0
 
  Keywords : How to use plinq / parallel linq queries in c# / vb.net with examples, Plinq queries in c# / vb.net with examples, Plinq / Parallel linq in c# / vb.net to increase performance of application

Here we will learn PLINQ or Parallel LINQ in c#, vb.net with example and how to use parallel linq in asp.net applications using c#, vb.net with examples.

PLINQ (Parallel LINQ)

PLINQ stands for Parallel LINQ. Parallel LINQ (PLINQ) is a parallel implementation of LINQ to Objects. PLINQ provides support for Parallel programming and is closely related to the Task Parallel Library. In very simple words, PLINQ enables a query to automatically take advantage of multiple processors. PLINQ can significantly increase the speed of LINQ to Objects queries by using all available cores on the host computer more efficiently. This increased performance brings high performance computing power onto the desktop.

Syntax of PLINQ

Following is the syntax of using PLINQ to increase the performance of LINQ queries in c#, vb.net.

 

C# Code

 

IEnumerable<int> rvals = Enumerable.Range(1, 100000000);

var result1 = rvals.AsParallel().Where(x => x % 12345678 == 0).Select(x => x);

VB.NET Code

 

Dim rvals As IEnumerable(Of Integer) = Enumerable.Range(1, 100000000)

Dim result1 = rvals.AsParallel().Where(Function(x) x Mod 12345678 = 0).[Select](Function(x) x)

If you observe above syntax we used Parallel method with LINQ queries to increase performance of LINQ queries.

Example of PLINQ

Following is the example of PLINQ in C# / VB.NET.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

// Using LINQ

IEnumerable<int> rvals = Enumerable.Range(1, 100000000);

var result = rvals.Where(x => x % 12345678 == 0).Select(x => x);

Stopwatch sq = Stopwatch.StartNew();

foreach (var item in result)

Console.WriteLine(item);

Console.WriteLine("Time taken for processing: " + sq.ElapsedMilliseconds + "ms");

// Using PLINQ

var result1 = rvals.AsParallel().Where(x => x % 12345678 == 0).Select(x => x);

sq.Restart();

foreach (var item in result1)

Console.WriteLine(item);

Console.WriteLine("Time taken for processing: " + sq.ElapsedMilliseconds + "ms");

Console.ReadLine();

}

}

}

VB.NET Code

 

ModuleModule1

Sub Main()

' Using LINQ

Dim rvals As IEnumerable(Of Integer) = Enumerable.Range(1, 100000000)

Dim result = rvals.Where(Function(x) x Mod 12345678 = 0).[Select](Function(x) x)

Dim sq As Stopwatch = Stopwatch.StartNew()

For Each item In result

Console.WriteLine(item)

Next

Console.WriteLine("Time taken for processing: {0} ms", sq.ElapsedMilliseconds)

' Using PLINQ

Dim result1 = rvals.AsParallel().Where(Function(x) x Mod 12345678 = 0).[Select](Function(x) x)

sq.Restart()

For Each item In result1

Console.WriteLine(item)

Next

Console.WriteLine("Time taken for processing: {0} ms", sq.ElapsedMilliseconds)

Console.ReadLine()

End Sub

End Module

If you observe above example, we specified a range of integer numbers starting from “1” to “100000000”. We are dividing numbers in range to find out numbers which are get divided by “12345678” and calculating how much time its taking to finish division of all numbers for normal LINQ and PLINQ.  Here we used Stopwatch to calculate the time taken to perform this task.

Output of PLINQ Example

Following is the result of PLINQ example.

 

PLINQ / Parallel LINQ Example in C#, VB.NET with Examples

 

This is how we can use PLINQ or Parallel LINQ in c#, vb.net to improve the performance of LINQ queries.

 

PREVIOUS

LINQ to XML
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User