I am scanning the MediaStore
on app start. I iterate over it for audio tracks and store them. I also store other aspects like albums. I store the tracks in an ArrayList
and that also goes for albums (different ArrayList
) This works fine but I have started to consider the performance.
I want to keep track of tracks count, for each album, while scanning. So, I originally used a HashMap
of album id -> tracks count. With each track I get during scanning, I check if the HashMap
(associated with the album id) exists or not. I create it when it does not exist and update it with the count otherwise. Then, after the scanning, I would iterate over the albums ArrayList
and update the entries with the information from HashMap
by using the HashMap
lookup.
I am starting to wonder if it is worth it to use HashMap
here for performance. Will skipping the HashMap
and looking up the album I want to update, using a for loop, then updating it, be better in performance?
Thanks.
Edit: since I cannot post the code because it is a large chunk, so I will simplify it. Original code:
private void scanTracks()
{
HashMap<Long, Integer> albumHashMap= new HashMap<>();
// Iterate over tracks in MediaStore Block Start
// Get albumId.
if (!albumHashMap.containsKey(albumId))
albumHashMap.put(albumId, 1);
else
albumHashMap.put(albumId, albumHashMap.get(albumId) + 1);
// Iterate over tracks in MediaStore Block End
for(Album album : mAlbums)
{
album.setCount(albumHashMap.get(album.getId()));
}
}
Here is the alternative implementation I am suggesting.
private void scanTracks()
{
// Iterate over tracks in MediaStore Block Start
// Get albumId.
for (Album album : mAlbums)
{
if (album.getId() == albumId)
{
album.secCount(album.getCount()+1);
break;
}
}
// Iterate over tracks in MediaStore Block End
}
I am scanning the MediaStore on app start. I iterate over it for audio tracks and store them. I also store other aspects like albums. I store the tracks in an ArrayList and that also goes for albums (different ArrayList) This works fine but I have started to consider the performance.
With this description, I understand that you have a list of all the tracks, which is not grouped by album. Then, you have another list of all the albums.
If that's the case, with the use of HashMap
, the time complexity will be O(n + m)
, where n and m is the count of tracks and albums, respectively. Haven't evaluated the performance of the other way, I can say that this is a sign of good performance.
Now, if you go through each album in the album list, and then iterate the tracks to count, the time complexity would be O(m*n)
, which is far worse than the above O(m + n)
.
So in recap, use HashMap
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments