1

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?.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 2
    http://stackoverflow.com/questions/7582591/how-to-plinq-an-existing-linq-query-with-joins Your string methods aren't complex enough, so bad PLINQ candidates. Every operation needs too less time. So the PLINQ overhead is higher than the gains. – Tim Schmelter Feb 09 '16 at 14:13
  • Thankyou. In other words, you mean I should take into account only the expensiveness of each function of the **SelectMany** instead of how much big the source or the return "data" (the collection) it is or it would be at the end?. This article also helped me but only a little bit: https://msdn.microsoft.com/en-us/library/dd997399%28v=vs.110%29.aspx – ElektroStudios Feb 09 '16 at 14:40
  • 1
    The more time a function/operation needs that you want to parallelize, the better candidate it is. – Tim Schmelter Feb 09 '16 at 14:55

0 Answers0