Home » Android » Android – dictionary file. Which is faster, database or reading file directly?

Android – dictionary file. Which is faster, database or reading file directly?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m making an dictionary app for android. This app uses stardict and DICT files which is are often pretty large (10MB or more) and contains just plain texts.

When users look up a word, my app will read the file randomly and return the result. I’ve read on some articles that reading file is expensive and slow. So I wonder if bringing all data into database is a better solution?

How to&Answers:

I would suggest putting your words into a database for the following reasons:

  1. DB lookups on android with SQLite are “fast enough” (~1ms) for even the most impatient of users
  2. Reading large files into memory is a dangerous practice in memory-limited environments such as android.
  3. Trying to read entries out of a file “in place” rather than “in memory” is effectively trying to solve all the problems that SQLite already solves for you.

The only challenge presented in using a database is initializing it with the data you require. This problem can best be solved by creating the desired database in advance and then attaching it to your APK assets. There is an example here that is quite good.

Hope this helps.