Dmytro
Dmytro

Reputation: 402

How to speed up parcing data from .txt and writing to SQLite in Android?

I'm trying to parce data from .txt file and then write it into a database. I need to have one word and it's first letter in a row. Here is how I work with text file:

private String convertStreamToString(InputStream is)
        throws UnsupportedEncodingException {

    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    String line;
    try {
        while (true) {
            line = reader.readLine();
            if (line == null) {
                break;
            }
            allCities = stringBuilder.append(line).toString();
        }
    } catch (IOException e) {
        Log.e("error", e.getMessage());
    }
    return allCities;
}


private String getAllWordsString() throws IOException {

    AssetManager am = context.getAssets();
    InputStream is = am.open(Constants.FILE_NAME);
    allWords = convertStreamToString(is);
    is.close();

    return allWords;
}

And here is how I write it to a databate:

public void inputDBFeed(MyWord model) {
    ContentValues cv = new ContentValues();
    cv.put(Constants.COLUMN_NAME, model.getName());
    cv.put(Constants.COLUMN_FIRST_LETTER, model.getFirstLetter());
    database.insert(Constants.MAIN_DB_NAME, null, cv);
}

 public void prepareDBFeed() {

    try {
        allWords = this.getAllWordsString();
    } catch (IOException e) {
        Log.e("Error:", e.getMessage());
    }

    String[] cities = allWords.split(" ");
    for (String oneWord : words) {
        App.getDBManager().inputDBFeed(new Word(oneWord, getFirstLetter(oneWord)));
    }

}

But running this process on a device takes from 2 to 5 min. Please offer me a way tospeed it upas much as possible.

Upvotes: 0

Views: 119

Answers (1)

laalto
laalto

Reputation: 152817

  1. Profile your code to see where the time is really spent and whether any change really helps.

  2. Wrap several hundred inserts in a database transaction to avoid waiting for disk I/O on each insert.

  3. Use prepared SQL statements to avoid recompiling the same SQL over and over again.

  4. Do not convert your StringBuilder to string for each iteration - you need the value only after the loop.

  5. Possibly several other micro optimizations such as avoiding allocs and caching values whenever possible.

Upvotes: 1

Related Questions