Android Question Performance List v/s Map

nikolaus

Member
Licensed User
Longtime User
Hi, Wyken Seagrave writes in his B4A book that reading (and writing?) an item from a Map is much faster than from an Array.

How about Lists? Are they rather Maps with fixed keys (Indices) or Arrays with dynamic size?

Does it make sense to feed larger ListViews (about 100 to 200 lines) fom a Map with index-numbers as keys rather than from a List?

Thx
Nikolaus
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim dtstart, dtend As Long
    Dim total As Period
    Dim testlist As List
    testlist.Initialize
    For i = 0 To 500000
        testlist.Add("Line "&i)
    Next
    dtstart = DateTime.Now
    lv.Clear
    For i = 0 To testlist.Size-1
        lv.AddSingleLine(testlist.Get(i))
    Next
    dtend = DateTime.Now
    total = DateUtils.PeriodBetween(dtstart,dtend) 
    Log("Difflist="&total.Hours&":"&total.Minutes&":"&total.Seconds&".")
 
    lv.Clear
    Dim testmap As Map
    testmap.Initialize
    For i = 0 To 500000
        testmap.Put(i,"Line "&i)
    Next
    dtstart = DateTime.Now
    lv.Clear
    For i = 0 To testmap.Size-1
        lv.AddSingleLine(testmap.Get(i))
    Next
    dtend = DateTime.Now
    total = DateUtils.PeriodBetween(dtstart,dtend) 
    Log("Diffmap="&total.Hours&":"&total.Minutes&":"&total.Seconds&".")

Filling 500.000 items to a LIST and put them in a listview needs 6 seconds on my device.
Filling 500.000 items to a map and put them in a listview needs 4 seconds on my device.

I would say using a MAP is faster but i dont think that on 100 to 1000 items should make no real difference in time... with such less amount of items you can use what you want i suppose
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you are using a list and feel it's slow, then test it with a map and measure the difference. I have an app that builds a listview from a list with > 2000 entries and rebuilds it in real-time filtering the original list. The performance is very good.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
That's not enough time to make a cup of coffee :(
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
With B4A as Development-Environment you dont have time for anything cause it is so fast...

That's very true, I just wish I could design faster:rolleyes:
 
Upvote 0

nikolaus

Member
Licensed User
Longtime User
Thanks Manfred for evaluating this.

Seagrave writes using a Map is very fast compared to an Array, that's why I was asking. A performance ratio of 2:3 is far away from that.
 
Upvote 0

sagyrd

New Member
Licensed User
Longtime User
I have notice low performance of a Map,when exchanging data between Activities.
Example:
'Activity1
Sub Process_Globals
Dim Cache as Map
End Sub
'Activity2
Activity1.Cache.Get("whatever")

Copying map solves this problem :
Cache = Activity1.Cache
 
Upvote 0

sagyrd

New Member
Licensed User
Longtime User
I see you point, but creating this reference instead of accessing it directly improves performance a lot
 
Upvote 0

sagyrd

New Member
Licensed User
Longtime User
I have to apology here.
I have created a small project like you asked and I can't recreate this issue.
The project contains very same files and doing exactly the same what my original code is doing. The only difference is that instead of using list of files, I was iterating through sql query result.
In both project performance is very good right now. I have detailed tested it yesterday(I was logging time of every action, and definitely performance drop was until accessing map ) and without creating references, performance was very low (it took about 5 seconds to load images) and I'm 100% sure that the delay was caused when I was accessing map directly (Activity1.Cache). After creating a reference, the speed increased drastically to the current level(few 'ticks').
I'm using Samsung Galaxy Tab 3 Lite for debugging. Maybe this issue shows up when device memory is full ?
 

Attachments

  • MapExample.zip
    213 KB · Views: 207
Upvote 0
Top