B4J Question why is this so slow?

madru

Active Member
Licensed User
Longtime User
hi,

can somebody answer this ?

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

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

Attachments

  • eval.zip
    1.8 KB · Views: 232
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime 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:
B4X:
Dim ll As List = perm.permute("123456789")
Dim l As List
l.Initialize
l.AddAll(ll)

Now it will be fast.
 
Upvote 0
Top