B4J Question why is this so slow?

Discussion in 'B4J Questions' started by madru, Nov 28, 2017.

  1. madru

    madru Active Member Licensed User

    hi,

    can somebody answer this ?

    why is this so slow or how to speed this up?

    Code:
    Dim dt,dt1 As Long
        dt=
    DateTime.now
        
    Dim l As List = perm.permute("123456789")
        dt1=
    DateTime.now
        
    Log("Permutations "& DateUtils.PeriodBetween(dt,dt1).Minutes&":"&DateUtils.PeriodBetween(dt,dt1).Seconds)
        dt=
    DateTime.now

        
    For i = 0 To l.Size -1
            
    Dim RowCol As List
            RowCol.Initialize
            RowCol=
    Regex.Split("",l.Get(i))
            
    If RowCol.Get(0)+RowCol.Get(1)+RowCol.Get(2) = 23 And _
            RowCol.Get(
    3)+RowCol.Get(4)+RowCol.Get(5) = 15 And _
            RowCol.Get(
    6)+RowCol.Get(7)+RowCol.Get(8) = 7 And _
            RowCol.Get(
    0)+RowCol.Get(3)+RowCol.Get(6) = 11 And _
            RowCol.Get(
    1)+RowCol.Get(4)+RowCol.Get(7) = 20 And _
           RowCol.Get(
    2)+RowCol.Get(5)+RowCol.Get(8) = 14 Then
           dt1=
    DateTime.now
                
    Log("Rows "& DateUtils.PeriodBetween(dt,dt1).Minutes&":"&DateUtils.PeriodBetween(dt,dt1).Seconds)
               dt1=
    DateTime.now
            
    End If    
        
    Next
        
    Log("all Rows "& DateUtils.PeriodBetween(dt,dt1).Minutes&":"&DateUtils.PeriodBetween(dt,dt1).Seconds)
    on Windows:
    Permutations 0:2
    Rows 3:48
    all Rows 3:48


    on OSX:
    Permutations 0:3
    Rows 9:41
    all Rows 9:41
     

    Attached Files:

    Last edited: Nov 28, 2017
  2. Erel

    Erel Administrator Staff Member Licensed User

    Can you upload the program?
     
  3. madru

    madru Active Member Licensed User

    done
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    That was interesting.

    It was indeed surprisingly slow. The reason is that jPermutation library returns a linked list. Such lists are very slow if you try to access them by index.

    The solution is to add all items to a new list:
    Code:
    Dim ll As List = perm.permute("123456789")
    Dim l As List
    l.Initialize
    l.AddAll(ll)
    Now it will be fast.
     
  5. madru

    madru Active Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice