Code Bytes #1 – PLINQ Basics

PLINQ /Parallel LINQ is part of the TPL (Task Parallel Library) and it makes your life easier when it comes to multi-core processor programming which is totally different from multithreading which allows more than one thread per process and you have no idea if they will be equally distributed across CPU cores. To use PLINQ your objects have to be in memory. This means you can’t use AsParallel on LINQ to SQL until you bring all your query results over to the local machine. When it comes to running your code in parallel the key to remember is that the AsParallel method is you friend. Every result that gets returned after your first call to AsParallel is always a ParallelQuery object. You can get more theory here. Now go code!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace OliverCode.CodeBytes
{
    class ParallelLinqCB
    {
        static void Main(string[] args)
        {
            Action action = (int itemFromList) => Console.Write(itemFromList + ",");
            var lst = Enumerable.Range(1, 10);

             //PLINQ will decide on the number of processors to use in this run up to 64 threads (if you have that much)
             lst.AsParallel().Select(i => i * i).ForAll(action);
            Console.WriteLine();
            //My machine has 4 cores but i only need it to use up to 2 cores so I use the WithDegreeOfParallelism to restrict it
            //Or I could have use all 4 with the statement WithDegreeOfParallelism(4) hereby not letting PLINQ choose for me
            lst.AsParallel().WithDegreeOfParallelism(2).ForAll(action);

            Console.Read();
        }
    }
}

Cropped diagram courtesy of MSDN PFX (Parallel Programming Framework)
Code Bytes 1 - PLINQ

Advertisements

Dynamic Programming in C# 4.0 – An Overview

One of the most interesting additions to C# 4.0, I think, is the dynamic addition. Just thinking about this makes me excited. I will jump right into a little theory then some code.

The Theory of Dynamic

So what is this here dynamic thingy? dynamic in C# 4.0 refers to dynamic binding and dynamic binding is what happens at runtime and not at compile time. This involves binding a method, property, operator, member etc of an object at runtime. Yes I know this sounds like polymorphism or like the var keyword or even like using the ultimate base class in C# – object. First and foremost you have to let go or what you know and remember this important fact.

dynamic != var && dynamic != object

The keyword dynamic, casually speaking, tells the compiler that “Even though it cannot resolve the member statically, it should trust the programmer and don’t worry because it will/may be resolved at runtime.”

Code

Here is a sample on how you use the dynamic keyword:

dynamic dyn = “My first time”;
Console.WriteLine(dyn);

Now let’s look at some similarities and differences of var, object, and dynamic for a sec.

var v = 1; // here the compiler will figure out (at compile time) the type for v which will be int.
Console.WriteLine(v.GetType().ToString());
//v.Compute(); // causes a compiler error
object o = 1; // this is boxed from value type to an object with type being int32
Console.WriteLine(o.GetType().ToString());
//o.Compute(); //also gives a compiler error
dynamic d = 1; //type here is int32
Console.WriteLine(d.GetType().ToString());
d.Compute(); // does not give compile time error but will throw a runtime RuntimeBinderException

Continue reading