WOW!!! When I first saw it.
Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4
http://www.microsoft.com/downloads/details.aspx?FamilyID=86b3d32b-ad26-4bb8-a3ae-c1637026c3ee&displaylang=en
You must must read it!!!
Namoskar!!!
If you want to assign Nothing to the optional parameter in VB.NET 10, it is just like to obvious now,
Sub Main()
'Passing value for the optional parament _age
MyFunc("Wriju", "wriju@contoso.com", 30)
'No value is supplied for the optional parament _age
MyFunc("Writam", "writam@contoso.com")
End Sub
Sub MyFunc(ByVal _name As String,
ByVal _email As String,
Optional ByVal _age As Integer? = Nothing)
Console.WriteLine("Name={0}, Email={1}, Age={2}",
_name, _email, _age.ToString())
With implicit line continuation VB.NET 10 now allows you to write the multiline Lambdas. That means like your normal Functions you can write functions under Lambdas.
So now you may write like,
Dim arrInt As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim myFinc = Array.FindAll(arrInt, Function(n)
If n Mod 2 = 0 Then
Console.WriteLine("{0} is Even", n)
Else
Console.WriteLine("{0} is not Even", n)
End If
End Function)
Visual Studio 2010 has new API for LINQ (Language Integrated Query). This helps us to implement the power of Parallel Computing in declarative manner.
LINQ without Parallel Extension
It will take 3 sec for 28 thousand elements.
static void Main(string[] args)
{
Stopwatch sw = Stopwatch.StartNew();
DoIt();
Console.WriteLine("Elapsed = " + sw.ElapsedMilliseconds.ToString());
Console.ReadLine();
}
private static void DoIt()
IEnumerable<int> arrInt = Enumerable.Range(1, 4000000);
var q =
from n in arrInt
where IsPrime(n)
select n.ToString();
List<string> list = q.ToList();
Console.WriteLine("Elements : " + list.Count.ToString());
private static bool IsPrime(int p)
//Find the prime number
int upperBound = (int)Math.Sqrt(p);
for (int i = 2; i < upperBound; i++)
if (p % i == 0) return false;
return true;
LINQ with Parallel Extension
With a very simple change in query this will take around 1.5 sec for the same number of elements.
from n in arrInt.AsParallel()
Greenroom
What’s the magic? When we use .AsParallel() it does cast the list of integers into ParallelEnumerable class and calls a different Where method which implements the new “Task” API.
With Lambda expression this would look more clear.
Before
var q = arrant
.Where(n => IsPrime(n))
.Select(n => n.ToString());
Class: Enumerable
Method:
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
After (while using AsParallel())
Code:
var q = arrInt.AsParallel()
Class : ParallelEnumerable
Method :
public static ParallelQuery<TSource> Where<TSource>(this ParallelQuery<TSource> source, Func<TSource, bool> predicate);
The above time will vary based on CPU power and availability.