Archive for December, 2011

Blog has moved! You should check this post out on kevinpelgrims.com

As I’m looking at CoffeeScript in my spare time, I wanted a fast way to build the scripts. Since I work with Sublime Text 2 for all my scripting, it seemed like the right tool for the job.

Sublime is a text editor that has a lot in common with TextMate for OS X, but it has the huge advantage of working cross-platform. Sublime can also be extended using plug-ins, like TextMate, and a lot of TextMate bundles are compatible with it.


Now, there are several ways to compile CoffeeScript locally (e.g. from .NET or using the built-in watch option). We are going to use Node.js and npm (the Node Package Manager). Node.js works on Windows these days and the .msi installer also makes sure npm is on your system. You will need to add the folder that contains (default is C:\Program Files (x86)\nodejs) node.exe and npm.cmd to your environment variables to make sure you can easily run them from anywhere. Here’s a nice guide to adding, removing and editing environment variables in Windows 7. The normal thing to do here, is to add it to the “path” variable, if it’s not there already, feel free to create it.

CoffeeScript compiler

Installing the CoffeeScript compiler and Node.js tools is pretty straight forward when we open up a command window and use npm:

npm install coffee-script

Warning: be sure to open the command window on the same folder as your Node.js installation, that means where node.exe lives (default is C:\Program Files (x86)\nodejs). You will need to open the command windows as administrator to do this. Because npm will not use an absolute path to create new folders, but will use ./node_modules. So it will create a new folder in a location you don’t want it to be if you don’t pay attention.

This installs CoffeeScript stuff for Node.js and on Windows it has a .cmd to run coffee-related stuff through Node.js automatically. The default folder for these .cmd files is C:\Program Files (x86)\nodejs\node_modules\.bin and you should add this to your environment variables too (preferably the existing “path” variable). After doing all that, you should be able to run “coffee” in your command window.

CoffeeScript in Sublime

To get CoffeeScript support in Sublime, we’re going to download the CoffeeScript package. First off, it’ll be useful to install Sublime Package Control, to make it easier to install packages. Detailed instructions can be found here: http://wbond.net/sublime_packages/package_control/installation

Now, when we restart Sublime, we can access package control through the Command Palette using Ctrl+Shift+P. As you start typing commands, you get an overview of the available options. If you execute “discover packages” a browser pops up giving you a nice overview of all available packages and a search bar. To install a package, we need the command “install package”. After pressing enter we get a list of all available packages. We will install “CoffeeScript”.

Building it

After doing all that we already have some support for the language, i.e. syntax highlighting. But building the scripts will not yet work on Windows. To fix this, click Preferences –> Browse Packages… in Sublime. This will open an explorer window where you can see all the packages. Navigate to CoffeeScript\Commands and open the file CoffeeScript.sublime-build. Remove the line that has the path variable and change the cmd line to have coffee.cmd instead of coffee.

Original version:

"path": "$HOME/bin:/usr/local/bin:$PATH",
"cmd": ["coffee","-c","$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.coffee"

Edited version:

"cmd": ["coffee.cmd","-c","$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.coffee"

Now create a file, save it as test.coffee, add some CoffeeScript, press Ctrl+B and Sublime creates a file called test.js in the same folder.

CoffeeScript file:

console.log "Hello world!"

JavaScript file:

(function() {

console.log("Hello world!");


You can adjust the build settings to your own needs, e.g. change the output directory or include JSLint. But for getting to know CoffeeScript and playing with it locally, this will work. And it makes it a lot easier! We can also for example integrate building LESS files to CSS in the same way. That would provide a uniform way of building different scripts and styles in a project and prevents us from having to use the command line to do everything manually, which would slow us down.

Blog has moved! You should put your comments on and link to kevinpelgrims.com


Read Full Post »

Blog has moved! You should check this post out on kevinpelgrims.com

Now that we’ve covered the Task Parallel Library, it’s time to move on.

What is PLINQ?

PLINQ stands for Parallel LINQ and is simply the parallel version of LINQ to Objects. Just like LINQ you can use it on any IEnumerable and there’s also deferred execution. Using PLINQ is even easier than using the Task Parallel Library!

Regular for loop and LINQ compared to PLINQ (with time in seconds)

How do we use PLINQ?

You can even make existing LINQ queries parallel simply by adding the AsParallel() method. That’s how easy it is! This makes it easy to use the power of parallelization, while enjoying the readability of LINQ. Isn’t that great?

var employees = GetEmployees();

// Regular LINQ
var query = employees.Select(e => e.Skills.Contains("C#"));

// Extension method style PLINQ
var queryParallel1 = employees.AsParallel()
                              .Select(e => e.Skills.Contains("C#"));

// Query expression style PLINQ
var queryParallel2 = from e in employees.AsParallel()
                     where e.Skills.Contains("C#")
                     select e;

Important fact: PLINQ uses all the processors of your system by default, with a maximum of 64. In some cases you might want to limit this, to give a machine some more power to take care of other tasks. Everybody deserves some CPU time! So don’t be greedy and use WithDegreeOfParallelism() on heavy queries. Following example uses a maximum of 3 processors, even if there are 16 available.

var queryDegree = employees.AsParallel()
                           .Select(e => e.Skills.Contains("C#"));

By default PLINQ doesn’t care about the order of your output, compared to the input. This is because order preservation costs more time. You can enable order preservation though, again in a very simple way, by using the AsOrdered() method. It’s good to know that OrderBy() will also take care of order preservation.

var employees = GetEmployeesOrderedByName();

var queryOrdered = employees.AsParallel()
                            .Select(e => e.Skills.Contains("C#"))

We want more!

PLINQ has a lot more to offer than what we talked about here, so be sure to use Google and MSDN if you want to know more. Check out this “old” (2007) yet interesting article on PLINQ from MSDN magazine. An important read is Understanding Speedup in PLINQ on MSDN, which explains a bit more of how PLINQ works and why it sometimes defaults to sequential mode anyway.

Read Full Post »

Blog has moved! You should check this post out on kevinpelgrims.com

I have talked about parallel programming in .NET before, very briefly: Parallel programming in .NET – Introduction. This follow-up post is long overdue 🙂

What is the TPL?

The Task Parallel Library is a set of APIs present in the System.Threading and System.Threading.Tasks namespaces. The point of these APIs is to make parallel programming easier to read and code. The library exposes the Parallel.For and Parallel.ForEach methods to enable parallel execution of loops and takes care of spawning and terminating threads, as well as scaling to multiple processors.

How do we use the TPL?

Following code uses the sequential and the parallel approach to go over a for-loop with some heavy calculations. I use the StopWatch class to compare the results in a command window.

watch = new Stopwatch();
for (int i = 0; i < 20000; i++)
Console.WriteLine("Sequential Time: " + watch.Elapsed.Seconds.ToString());

watch = new Stopwatch();
System.Threading.Tasks.Parallel.For(0, 20000, i =>
Console.WriteLine("Parallel Time: " + watch.Elapsed.Seconds.ToString());

The result of running this on my laptop (with multiple cores) looks like this:

Result of comparison sequential - parallel

As you can see, the parallel for-loop runs A LOT faster than the sequential version. By using all the available processing power, we can speed up loops significantly!

Below is a screenshot of the task manager keeping track of what’s happening  while executing the sequential and the parallel. What we can see here is that at first (where the red arrow is pointing at) we only use 1 core heavily. When the parallel code kicks in, all cores peak.

Task manager during comparison sequential - parallel

So, looking at the above code, implementing all this parallelism doesn’t seem to be that hard. The TPL makes it pretty easy to make use of all the processors in a machine.

Creating and running tasks

It’s possible to run a task implicitly by using the Parallel.Invoke method.

Parallel.Invoke(() => DoSomething())
Parallel.Invoke(() => DoSomething(), () => DoSomethingElse())

All you need to do is pass in a delegate, using lamba expressions makes this easy. You can call a named method or have some inline code. If you want to start more tasks concurrently, you can just insert more delegates to the same Parallel.Invoke method.

If you want more control over what’s happening, you’ll need to use a Task object, though. The task object has some interesting methods and properties that we can use to control the flow of our parallel code.

It is possible to use new Task() to create a new task object, but it’s a best practice to use the task factory. (Note that you can’t use the task factory if you want to separate the creation and the scheduling of the task.)

// Create a task and start it
var task1 = new Task(() => Console.WriteLine("Task1 says hi!"));

// Create a task using the task factory
var task1 = Task.Factory.StartNew(() => Console.WriteLine("Task1 says hi!"));

You can also get results from a task, by accessing the Result property. If you access it before the task is completed, the thread will be blocked until the result is available.

Task<int> taskreturn = Task.Factory.StartNew(() =>
    int calc = 3 + 3;
    return calc;
int result = taskreturn.Result;

To be continued..

You can chain tasks by using the Task.ContinueWith method. It’s also possible to access the result of the preceding task in the next one, using the Result property.

// Regular continuation
Task<int> task1 = Task.Factory.StartNew(() => 5);
Task<string> task2 = task1.ContinueWith(x => PrintInt(x.Result));

// Chained continuation
Task<string> task1 = Task.Factory.StartNew(() => 5)
                     .ContinueWith(x => PrintInt(x.Result));

The methods ContinueWhenAll() and ContinueWhenAny() make it possible to continue from multiple tasks by taking in an array of tasks to wait on and the action to be undertaken when those have finished. More about those functions can be found on MSDN.

The force is strong with this one

We only looked at a few functions of the TPL and I think it’s clear this is a very powerful library. When working on applications that need a lot of processing power, parallel programming in .NET can make it easier to improve performance, a lot.


Of course there is a lot more to TPL than covered in this small introduction, so go ahead and explore!

Read Full Post »