You are on page 1of 4

In our quest to count all the stars in the universe, let’s first create a data structure for the

star and
add to the universe, and let us use the good ol` mother of all loops the “FOR” Loop, and see how
much inefficient this loop has become these modern days!!

“The Sequential execution took almost 30 seconds in my Dual Core Computer.”


And here is the Parallel Computing version of the same method. Yes, the for loop has been
replaced with Parallel.For a new entry in System.Threading namespace.
How simpler can this get to?
VOILA! The Parallel execution took Just 3 Seconds in my Dual Core Computer.

Well, That’s a significant performance improvement without Hardware Scale-out or Scale-up, all
we are doing is using the existing hardware resource efficiently. So much to a FOR Loop J, Huh.
30 Seconds of execution have become 3 seconds instantly. Look closer to the screenshot – the
stars are not counted sequentially, instead it allocates the task to the available CPU in parallel.

Because the loop is run in parallel, each iteration is scheduled and run individually on whatever
core is available. This means that the list is not necessarily processed in order, which can
drastically impact your code. You should design your code so that each iteration of the loop is
completely independent from the others. Any single iteration should not rely on another in order
to complete correctly.

You might also like