Scenario
I'm using this function below on which I would like to take advantage of parallelism (their supposed beneffits), however, when I try to implement a simple Parallel LINQ technique the results are very negative, I don't udnerstand why, but the total execution time increases around by 10 in comparison.
<DebuggerStepThrough>
Public Shared Function PermuteCharacters(ByVal charSet As Char(),
ByVal length As Integer,
ByVal allowRepetition As Boolean) As IEnumerable(Of String)
If (charSet.Count <> charSet.Distinct.Count) Then
Throw New ArgumentException("Char-set contains duplicated characters.", "charSet")
Return Nothing
End If
If (length = 1) Then
Return charSet.Select(Function(c As Char)
Return New String(New Char() {c})
End Function)
End If
If (allowRepetition) Then
Return PermuteCharacters(charSet, length:=length - 1, allowRepetition:=True).
AsParallel.AsOrdered.
SelectMany(Function(str As String)
Return charSet
End Function,
Function(str As String, c As Char)
Return str & c
End Function)
Else
Return PermuteCharacters(charSet, length:=length - 1, allowRepetition:=False).
AsParallel.AsOrdered.
SelectMany(Function(x As String) charSet,
Function(str As String, c As Char)
If Not str.Contains(c) Then
Return str & c
Else
Return Nothing
End If
End Function).
Where(Function(value As String) value IsNot Nothing)
End If
End Function
This simple code to test:
Dim permutations As IEnumerable(Of String) =
PermuteCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 5, allowRepetition:=True)
File.WriteAllLines("C:\test.txt", permutations)
Without parallelism it takes approx. 10 seconds.
With parallelism it takes approx. 50 seconds.
Question
I would like to understand why happens this, and also how to fix/improve it?.