November, 2009

  • Wriju's BLOG

    Patterns for Parallel Programming

    • 0 Comments

    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!!!

     

  • Wriju's BLOG

    VB.NET 10 : Optional Nullable Parameter

    • 0 Comments

    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())

    End Sub

     

    Namoskar!!!

  • Wriju's BLOG

    VB.NET 10 : Multiline Lambdas

    • 2 Comments

    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)

     

    Namoskar!!!

  • Wriju's BLOG

    Parallel Computing: PLINQ

    • 1 Comments

    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.

    var q =

        from n in arrInt.AsParallel()

        where IsPrime(n)

        select n.ToString();

     

    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()

        .Where(n => IsPrime(n))

        .Select(n => n.ToString());

     

    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.

    Namoskar!!!

Page 1 of 1 (4 items)