Reputation: 551
I'm currently working on a project in school (Android application) where we are supposed to store information on the users' expenses and income in a local SQLite database, and then display it as various diagrams, etc.
My question is; What would be faster? Constantly making queries to the database in order to get information on certain expenses, or simply making a query on startup and storing the returned values as objects in a list?
Thanks!
Upvotes: 3
Views: 2267
Reputation: 769
If you're gonna iterate through the objects/data, then probably best to use a Database to query.
Otherwise, use a List to make it faster and also simple to code.
Upvotes: 0
Reputation: 2518
It is faster to pull every entry from DB at once and then save it into the List till it is used.
Unless you want to search in those Entries. If you are looking for every Entry that looks like "market" i would search in db and put the result in the list to display it.
Basicly, what you want to avoid is to retrieve every Entry from your Database at exactly the Time, that the ListAdapter calls getView and you need new Data.
Upvotes: 5
Reputation: 7485
The answer is; it depends. If you have only a small number of items in the list, then yes it will almost certainly be faster to pull them at startup; however if there are thousands of items, it might make the application startup much slower (although accessing the individual items will of course be faster).
My advice would be to actually test it and see which approach works best with the data set you have, and make your decision based on empirical evidence.
Upvotes: 2
Reputation: 4335
Accessing a database is expensive. So it would make sense to get all the data out of the DB. If the data is changed, update your database in onStop (this is the place to do clean up work).
Upvotes: 3
Reputation: 1665
It really depends on the amount of data and the complexity of the queries you need to perform. If you are dealing with thousands of objects, and you need to iterate over the list to check for specific properties on each, it might well be faster to use a database.
Upvotes: 5
Reputation: 6476
Storing them as list will definitely be faster. Here is a breakdown
If you load data and keep it in list you will always have access to it since it is in the memory and is already in format you want.
If you query every single time then there are three steps involved.
1) Read data from memory (hard drive)
2) Convert it into an object you want
3) Store data in memory (RAM)
4) Read data from memory (RAM)
Too many steps and slower execution as you see
Upvotes: 5