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

相关问题