Transforming Sequential Code to Parallel Code

Until recently, most Visual Basic code was written with a sequential and synchronous execution approach. Therefore, a lot of algorithms have been designed with neither concurrency nor parallelism in mind. Typically, you won't find algorithms that can be completely converted to fully parallelized and perfectly scalable code. It could happen, but it represents an ideal situation and it isn't the most common scenario.

When you have sequential code and you want to take advantage of potential parallelism to achieve better performance, you have to find hotspots. Then you can convert them to parallel code, measure speedups, identify potential scalability, and ensure that you haven't introduced new bugs while transforming the existing sequential code to parallel code.

A hotspot is a part of the code that takes significant time to run. You can achieve speedups if it is split into two or more pieces running in parallel. If part of the code doesn't take significant time to run, the overhead introduced by TPL could reduce the performance improvement to worthless or even make the parallelized code run slower than the sequential version. Once you begin working with the different options offered by TPL, it is going to be easier for you to detect the hotspots in sequential code.

Detecting Hotspots

The code discussed in this section is from code file Listing01.sln and shows an example of a very simple console application that runs two sequential ...

Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.