Android Question Array versus List

Status
Not open for further replies.

RB Smissaert

Well-Known Member
Licensed User
Longtime User
In VB6 and VBA are always faster than for example collections and dictionaries.
I understand that in B4A arrays are internally converted to a List.
If that is the case is there any point in using an array instead of a List?
Is using a List actually faster?

RBS
 
I understand that in B4A arrays are internally converted to a List.
This is not correct. B4A can automatically convert an array to a list. However arrays are stored as arrays internally.

I agree with Luca. In most cases you should prefer lists over arrays. Note that I talk about it in the collections video tutorial: https://www.b4x.com/etp.html
 
Upvote 0
It's enough simple to check performance
Code: Copy
    Dim intI As Long
    Dim intJ As Long
    Dim longDateTime (7) As Long
    Dim intMaximum As Int = 1000000
   
    longDateTime (0) = DateTime.Now
   
    Dim intArray (intMaximum) As Int
    For intI = 0 To intMaximum - 1
        intArray (intI) = 1234
    Next
   
    longDateTime (1) = DateTime.Now   
   
    For intI = 0 To intMaximum - 1       
        intArray (intI) = intI * 2
    Next
   
    longDateTime (2) = DateTime.Now

    For intI = 0 To intMaximum - 1
        intJ = intArray (intI)
    Next
   
    longDateTime (3) = DateTime.Now
   
    Dim listTest As List
    listTest.Initialize
    For intI = 0 To intMaximum - 1
        Dim intValue As Int = 1234
        listTest.Add (intValue)
    Next
   
    longDateTime (4) = DateTime.Now
   
    For intI = 0 To intMaximum - 1
        listTest.Set (intI, intI * 2)
    Next
       
    longDateTime (5) = DateTime.Now

    For intI = 0 To intMaximum - 1
        intJ = listTest.Get (intI)
    Next
       
    longDateTime (6) = DateTime.Now
   
    Log ("Allocate : " & (longDateTime (1) - longDateTime (0)) & " vs " & (longDateTime (4) - longDateTime (3)) & "ms" & CRLF & _
         "Set : " & (longDateTime (2) - longDateTime (1)) & " vs " & (longDateTime (5) - longDateTime (4)) & "ms" & CRLF & _
        "Get : " & (longDateTime (3) - longDateTime (2)) & " vs " & (longDateTime (6) - longDateTime (5)) & "ms")

I made a test on Samsung Galaxy S4. Allocate : 7 vs 447 ms Set : 12 vs 1396 ms Get : 15 vs 443ms. So, performance of Lists is bad.
But Lists are very comfortable for developer. First of all, when number of elements is unclear.
 
Last edited:
Upvote 0
Ok, let's "allocate" will include initial value. Allocate : 15 vs 1274ms Set : 13 vs 1639ms Get : 15 vs 436ms
To add an element to list the program asks memory dispatcher. To ask allocation one time (array) is much faster than to ask 1 million times.
 
Upvote 0
This is the only test I did:
Code: Copy
Sub Test
    Dim intI As Long
    Dim longDateTime (3) As Long
    Dim intMaximum As Int = 1000000
  
    longDateTime (0) = DateTime.Now
    Dim intArray (intMaximum) As Int
    For intI = 0 To intMaximum - 1
        intArray (intI) = intI * 2
    Next

    longDateTime (1) = DateTime.Now
    Dim listTest As List
    listTest.Initialize
    For intI = 0 To intMaximum - 1
        listTest.Add (intI * 2)
    Next

    longDateTime (2) = DateTime.Now

    Log("Array time: " & (longDateTime(1) - longDateTime(0)) & "ms")       
    Log("List time: " & (longDateTime(2) - longDateTime(1)) & "ms")       
End Sub


Log:

Array time: 44ms
List time: 820ms

Ok, array was about 20 time faster - this means that if you need to cycle 1,000,000 on an array, you will save 776ms !

On some occasions this may be important, but often it is not, while the advantages of using Lists are important.
 
Upvote 0
when you start using heavy recursive routines 1.000.000 cycles is not much.

and you'll gain minutes to hours by just doing it the right way (arrays in most cases depending on how you lookup things).
 
Upvote 0
when you start using heavy recursive routines 1.000.000 cycles is not much.

and you'll gain minutes to hours by just doing it the right way (arrays in most cases depending on how you lookup things).

Thanks, did some testing and can see that arrays are indeed a lot faster.
Will stick to arrays for now then if there is any chance that the numbers of items may get big.

RBS
 
Upvote 0
ok, it doesn't work then. no problem in my case as I don't need a (working) GUI
 
Upvote 0
Status
Not open for further replies.
Back
Top