Question

I have some reference data in a text file (~5MB) that I want to use with might android application.

The file is of the format:

1|a|This is line 1a
1|b|This is line 1b
2|a|This is line 2a
2|b|This is line 2b
2|c|This is line 2c

What I want to know is the most efficient way (less memory, fast, size etc.) to use this file within my application.

a.) Should I save the file as a raw resource and open and read the whole file whenever I need a certain line.

b.) Should I convert the file to XML and use XPath to query the file when ever I need to look up a value

<!--sample XML -->
<data>
  <line number="1">
     <entry name="a">This is line 1 a</entry>
  </line>
</data>

c.) Should I just copy & paste the whole file as a static string array in the application and use that.

... any other suggestions are welcome.

[EDIT] I will also need to search this file and jump to arbitrary keywords e.g. "line 1a".

Was it helpful?

Solution

XML will always take longer to read than simple text or CSV files. What XML gives you in the tradeoff is a highly structured and reliable way of storing and retrieving data. XML files are, as you can see in the examples above, a good 2-3x larger than the data they actually contain.

If you're sure that you're never going to run into the "delimiter" character in your simple text file, then that would probably work just fine, purely from a file speed perspective.

OTHER TIPS

You have not provided enough information to answer this question. However, if I were a betting man, the answer is probably "none of the above".

I will also need to search this file

What does this mean? You are searching by some string key? By some regular expression? By a SQL-style query string where certain portions of a line are interpreted as integers versus strings versus something else? By a Google search-style string?

Each of those answers probably dictates a different technology for storing this information.

I will also need to...jump to arbitrary lines.

Why? How are you determining which "arbitrary lines" you are "jump"ing to: key? line number? byte offset? search results? something else?

And, of course, there are other questions, like:

  • How often is this data updated?
  • How is this data updated: new version of the app? download the whole file? download deltas/diffs? something else?
  • Is the data ASCII? UTF-8? Something else?

and so on.

Something that size that must be searched upon suggests "use a SQLite database", but some of the other answers might steer away from that solution.

If you are talking about very small amounts of data, the Android XML compiler can produce very efficient binary representations for you that you can access just like XML. On the other hand if the data is very large at all, and you need arbitrary queries, I would expect SQLlite to win out on performance (as well as flexibility). A small benchmark should be easy to write and would give you a good idea as to the basic tradeoffs involved.

Flat-files would be a last option, imo, but could work if the file isn't very large.

If you define efficiency as (less memory, fast, size etc.), a flat or delimited file will be faster to load and save.

However, people use XML because they are willing to trade some of that speed for XML's greater flexibility and ease of use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top