我想要应用程序向UserDictionary添加几个(2k)单词。将单词插入UserDictionary的最快方法
我已经试验了ContentResolver的()。插入/ BULK INSERT ....
// array of words
String[] words = getResources().getStringArray(R.array.word_array);
Long start,end = null;
start = System.currentTimeMillis();
int len = words.length;
ContentValues[] mValueArray = new ContentValues[len];
ContentValues mNewValues = new ContentValues();
mNewValues.put(UserDictionary.Words.APP_ID, "com.my.example");
mNewValues.put(UserDictionary.Words.LOCALE, "en");
mNewValues.put(UserDictionary.Words.FREQUENCY, "100");
for (int i = 0; i < len; ++i) {
mNewValues.put(UserDictionary.Words.WORD, words[i]);
mValueArray[i] = mNewValues;
}
getContentResolver().bulkInsert(
UserDictionary.Words.CONTENT_URI, // the user dictionary content URI
mValueArray // the values to insert
);
end = System.currentTimeMillis();
Toast toast = Toast.makeText(this, "Time for " + Integer.toString(len-1)+" words: " + Long.toString((end-start)) + "ms", 50);
toast.show();
在我的电话需要每字约100毫秒,如果我做的100 3+分钟批次bulkinsert插入2K话。
是否有人知道插入单词或可以完成的任何优化的更快方法?
侧面问题:UserDictionary大小是否存在上限或者是否取决于手机?
任何帮助表示赞赏
Michealster
试过了applyBatch,好像大概是同一时间。令人惊讶的是,无论采用何种方法,运行相同的代码都存在相当多的变化(〜+/- 10%)。正如@PravinCG所说,不管包装器如何,基本上不能使文件写入更快。 – user1094747 2012-02-16 08:16:06