Tech Blog‎ > ‎

.NET Parallel For - How Good Is It Really?

posted May 17, 2011, 1:51 PM by Victor Zakharov   [ updated May 17, 2011, 2:13 PM ]
I recently had a Microsoft workshop session and became excited about parallel for/foreach/etc.
There we got a solid 60% of efficiency. It means that whenever number of cores increases by 2x, performance is boosted by 1.6x.

I became curious about it, and created a test project to verify the fact for myself.
Here are the results I got on subsequent runs (time in milliseconds + efficiency):

I even got 96% on some early build, which did not show that information clearly (hence, no screenshot).

Two points from here:
1. Yes, performance is better on multi-core machines, exactly as expected.
2. No, it does not have 100% efficiency, and is in fact very dependent on Windows, with efficiency ranging from 27% to 96%.
It appears to be no way to predict efficiency factor at any given moment, so don't rely on it too much.

For reference, I am running on a Core 2 Duo E7200 @ 2.53 GHz, which has Windows performance rating of 6.1.
And this is the code (VB.NET console application, using framework 4.0):

Module Module1

  Const UBOUND As Integer = 100000000

  Public Sub AAA(n As Integer)
    Dim i As Integer = (n / 2)
  End Sub

  Sub Main()
    Dim st1 As New Stopwatch
    For n = 1 To UBOUND

    Dim st2 As New Stopwatch
    System.Threading.Tasks.Parallel.For(1, UBOUND, AddressOf AAA)

    MsgBox("Single threaded: " & st1.ElapsedMilliseconds & vbNewLine &
           "Multi-threaded: " & st2.ElapsedMilliseconds & vbNewLine &
           "Efficiency: " & Format((st1.ElapsedMilliseconds / st2.ElapsedMilliseconds - 1) * 100, "N") & " %")
  End Sub

End Module