Android Question How much (if at all) would this code or idea slow down the app/phone?

Daica

Active Member
Licensed User
So lets say I have a list of LatLng, maybe 100-500 items. Whenever the phone's GPS is changed, I iterate through the list to check the distance of the new location to every item in the list.
If the distance is <= X then execute some code, else, execute some other code.
Is this practice bad? The list is already going to be loaded with the LatLngs, so it won't change.

Example:

B4X:
Dim myLatLngs As List 'List holding anywhere from 50-500 LatLngs

'GPS location change event
Public Sub LocationChanged(Location1 As Location)
    For i = 0 To myLatLngs.Size - 1
        If MeasureDist(Location1, myLatLngs.Get(i)) <= 1600 Then
            'Run code if distance is less than 1600 meters
        End If
    Next
End Sub

I never went to school for programming so I have no idea if this is bad or not, big O notation or whatever :\
In my mind, it's a good idea, but I dont want to slow down the user's phone if there are 500+ LatLngs in the list each time they move.

I could add a check and only run through the list if they move a certain amount, but depending on GPS accuracy, this might not work all the time.
Ex:

B4X:
Dim LastLocation As LatLng

If MeasureDist(Location1, LastLocation) >= 500 Then 'only loop through list if they have moved more than 500 meters
    LastLocation = Location1
    For i = 0 To myLatLngs.Size - 1
        If MeasureDist(Location1, myLatLngs.Get(i)) <= 1600 Then
            'Run code if distance is less than 1600 meters
        End If
    Next
Else
    LastLocation = Location1
End If

What do you guys think?
 

agraham

Expert
Licensed User
Longtime User
I assume your app will be in the foreground interacting with the user when you do this. In which case just try it. The enemy of good programming is premature optimisation. Just write straightforward understandable code without being too clever, test it and then if need be optimise it later if necessary.

As GPS updates are maximum every one second on most Android phones a couple of hundred points should be fine on modern phones. You could arrange to only update a maximum number, say 100, of all the points points every GPS update and cycle through the whole batch over successive position updates. I could suggest some further more complicated options but - avoid premature optimisation!
 
Upvote 0

MikeSW17

Active Member
Licensed User
Longtime User
I agree with @agraham, "avoid premature optimisation".
But... in your design, it can make sense to consider optimizations you might want to make later.
In this case, maybe an example would be to store the co-ordinates sorted by ascending/descending Latitude (or Longitude).
Then in your nearness checking loop, you could bail-out and stop further checking when a sorted Lat (or Long) is larger (or smaller) [by some chosen amount] than the current GPS return.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
According to my experience:

the best way is to update the points (lat and lng) that are within a certain radius.

and update when the gps has moved a few meters or kilometers.
 
Upvote 0
Top