views:

1053

answers:

4

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".

+3  A: 

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.

routeNpingme
Your assumption that xml is inefficient is largely invalid on the Android platform. Presumably he will use the android resource compiler to compile this down to the (very efficient) android specific binary representation of XML.
jsight
A: 

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.

Rob Elliott
+3  A: 

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.

CommonsWare
It is just a reference file that is rarely updated.
Tawani
".jump to arbitrary lines" e.g. goto line "15|e"
Tawani
+1  A: 

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.

jsight
I would go for sqlite for large data too! It is very easy to index data in Sqlite.
tuinstoel