Android Question If "is" takes long time to process

ilan

Expert
Licensed User
Longtime User
hi

i have a list with different objects (type objects) and i make a check if an item IS x then do something.

i just noticed that a loop with about 300 items will take about 200ms. that's quite long for such a small list.

why dows the IF...IS statement takes so long?? is there a different way i could check if an item is a specific object and then do something??

just for comparison, a list with 1000 items without checking if IS and go through all in a loop will take less then 1ms.

thanx, ilan
 

stevel05

Expert
Licensed User
Longtime User
I've just tried it with very simple 1 value types and it takes 1 ms to run through 302 values on a Samsung j330:

B4X:
    Type T1(Val As Object)
    Type T2(Val As Object)

B4X:
    Dim L As List
    L.Initialize
    For i = 0 To 150
        Dim T As T1
        T.Initialize
        T.Val = i
        L.Add(T)
        Dim TY2 As T2
        TY2.Initialize
        TY2.Val = i
        L.Add(TY2)
    Next
  
    Dim T1Count, T2Count As Int
  
    Dim StartTime As Long = DateTime.Now
    For Each O As Object In L
        If O Is T1 Then
            T1Count = T1Count + 1
        Else
            T2Count = T2Count + 1
        End If
    Next
  
    Log("Time = " & (DateTime.Now - StartTime))
  
    Log(T1Count)
    Log(T2Count)

Are the types complex? or is the code doing anything major within the loop?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Testing for both types doesn't increase the time.

B4X:
    For Each O As Object In L
        If O Is T1 Then
            T1Count = T1Count + 1
        Else If O Is T2 Then
            T2Count = T2Count + 1
        End If
    Next
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I've just tried it with very simple 1 value types and it takes 1 ms to run through 302 values on a Samsung j330:

B4X:
    Type T1(Val As Object)
    Type T2(Val As Object)

B4X:
    Dim L As List
    L.Initialize
    For i = 0 To 150
        Dim T As T1
        T.Initialize
        T.Val = i
        L.Add(T)
        Dim TY2 As T2
        TY2.Initialize
        TY2.Val = i
        L.Add(TY2)
    Next
 
    Dim T1Count, T2Count As Int
 
    Dim StartTime As Long = DateTime.Now
    For Each O As Object In L
        If O Is T1 Then
            T1Count = T1Count + 1
        Else
            T2Count = T2Count + 1
        End If
    Next
 
    Log("Time = " & (DateTime.Now - StartTime))
 
    Log(T1Count)
    Log(T2Count)

Are the types complex? or is the code doing anything major within the loop?

hi steve, this is my type:

B4X:
    Type shopitem(id As Int,description As String, category As String, unit As String, quantity As Float, price As Double, note As String, status As Int, favorite As Boolean) 'status 0 = to buy, 1 = bought, 2 = doesnot exist

after some research i found that going through KVS db is maybe the reason it takes so long so what i do is i copy on activity_create all items to a map and then go through that map instead of the KVS map and it takes much faster.
from 220-250ms i went down to 10-15ms

the kvs stores such types in it and it seems for me that checking if an object is a type take longer then just go through a loop without such a check but the main reason it took so long was using the kvs instead of a simple map.

thank you for you help :)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
hmmmm.....

interesting results

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
1001
1: 2 ms
1001
2: 3 ms
** Activity (main) Resume **

i guess i found the reason now for sure, it is the KVS and not the IF ... IS

using instead a normal MAP will perform much faster as already mentioned in the post above :)
 

Attachments

  • ifistest.zip
    2.2 KB · Views: 171
Upvote 0
Top