我想要應用程序向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