1

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

回答

2

針對您的案例: 在字典中一次性添加2000個單詞需要時間,並且沒有太多優化可以完成。您在這裏看到的時間是用於文件操作並從Java-> NDK-> Native操作。

常規: 不管使用什麼方法,人們必須明白,這些只不過是文件操作而且必然需要時間。這些API(包括與SQLite相關的)只是本地代碼中預期文件操作的一個包裝。原生文件寫入總是需要較長時間才能讀取文件。

1

我在爲大型SQL查詢使用applyBatch方法了Android的源已經看到了。在作爲ContactsRemover的其他一些程序中,作者也使用這種方法。但我不知道它是否真的更快,然後插入或bultInsert。

+0

試過了applyBatch,好像大概是同一時間。令人驚訝的是,無論採用何種方法,運行相同的代碼都存在相當多的變化(〜+/- 10%)。正如@PravinCG所說,不管包裝器如何,基本上不能使文件寫入更快。 – user1094747 2012-02-16 08:16:06

相關問題