Android Question Variable Declaration and Assignment

mmieher

Active Member
Licensed User
Longtime User
Here's another thing I should probably know ..

Why is this really slow:
B4X:
For i = 0 to SomeList.Size - 1
     Dim qMap As Map = SomeList.get(i)
     Dim str As String = qMap.get("idkey")
Next

But this is super fast?
B4X:
For i = 0 to SomeList.Size - 1
     Dim qMap as Map
     qMap = SomeList.get(i)
     Dim str as String
     str = qMap.get("idkey")
Next

Marc
 

Mahares

Expert
Licensed User
Longtime User
Did you try something like these 3 variants to time them and reverse the order of operation:
B4X:
Dim t1 As Long =DateTime.now
    For i = 0 To MyList.Size - 1
        Dim qMap As Map
        qMap = MyList.get(i)
        Dim str As String
        str = qMap.get("idkey")
    Next
    Dim t2 As Long =DateTime.now
    Log(t2-t1)
    Log("----")
    
    Dim t1 As Long =DateTime.now
    For i = 0 To MyList.Size - 1
        Dim qMap As Map = MyList.get(i)
        Dim str As String = qMap.get("idkey")
    Next
    Dim t2 As Long =DateTime.now
    Log(t2-t1)
    Log("----")
       
    Dim t1 As Long =DateTime.now
    For Each qMap As Map In MyList
        Dim str As String
        str = qMap.get("idkey")
    Next
    Dim t2 As Long =DateTime.now
    Log(t2-t1)
 
Upvote 0

mmieher

Active Member
Licensed User
Longtime User
Did you try something like these 3 variants to time them and reverse the order of operation:
B4X:
Dim t1 As Long =DateTime.now
    For i = 0 To MyList.Size - 1
        Dim qMap As Map
        qMap = MyList.get(i)
        Dim str As String
        str = qMap.get("idkey")
    Next
    Dim t2 As Long =DateTime.now
    Log(t2-t1)
    Log("----")
   
    Dim t1 As Long =DateTime.now
    For i = 0 To MyList.Size - 1
        Dim qMap As Map = MyList.get(i)
        Dim str As String = qMap.get("idkey")
    Next
    Dim t2 As Long =DateTime.now
    Log(t2-t1)
    Log("----")
      
    Dim t1 As Long =DateTime.now
    For Each qMap As Map In MyList
        Dim str As String
        str = qMap.get("idkey")
    Next
    Dim t2 As Long =DateTime.now
    Log(t2-t1)
I can just visually see it in the logs. I have log statements. It's not that big a list.
 
Upvote 0

mmieher

Active Member
Licensed User
Longtime User
Look at the compiled Java to see if it is different for the two cases. It is possibly a Java compiler optimisation. As neither qMap nor str are used it might just be ignoring that code.
Will look. My posted example was just a snippet. In my Project, the variables are used. I couldn't figure out why the SearchView indexing thing went from lightning fast to plodding through the steps.
 
Upvote 0
Top