This page looks best with JavaScript enabled

.NET parallel for - how good is it?

 ·   ·  ☕ 2 min read

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):

performance results 1performance results 2performance results 3performance results 4

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):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
    st1.Start()
    For n = 1 To UBOUND
      AAA(n)
    Next
    st1.Stop()

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

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

End Module

Victor Zakharov
WRITTEN BY
Victor Zakharov
Web Developer (Angular/.NET)