将单词插入 UserDictionary 的最快方法

发布于 2025-01-05 17:17:52 字数 1314 浏览 3 评论 0原文

我希望应用程序向 UserDictionary 添加几个(2k)单词。

我已经尝试过 ContentResolver().insert/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 毫秒。插入 2k 个单词需要 3 分钟以上。

有谁知道插入单词的更快方法或可以进行的任何优化?

附带问题: UserDictionary 大小是否有上限或者是否取决于手机?

任何帮助感谢

迈克尔斯特

I would like app to add several (2k) words to UserDictionary.

I have experimented with ContentResolver().insert/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();

On my phone takes about 100ms per word if I do bulkinsert on batches of 100. 3+ minutes to insert 2k words.

Is anyone aware of a faster method to insert words or any optimization that can be done?

Side question: Is there an upper limit on UserDictionary size or does that depend on phone?

Any help appreciated

Michealster

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮湿 2025-01-12 17:17:52

具体针对您的情况:
一次性在词典中添加 2000 个单词需要时间,而且没有太多可以做的优化。您在这里看到的时间是文件操作和从 Java->NDK->Native 操作移动的时间。

一般的:
无论使用哪种方法,都必须明白这些只是文件操作,并且必然需要时间。 API(包括 SQLite 相关的)只是本机代码中预期文件操作的包装器。本机文件写入总是比文件读取花费更长的时间。

Specific to your case:
Adding 2000 words in the dictionary at a stretch will take time and there is not much of an optimization which can be done. The time you see here are for the file operation and moving from Java->NDK->Native operation.

General:
Irrespective of the method used, one has to understand that these are nothing but file operations and is bound to take time. The APIs (including SQLite related) are just a wrapper to the intended file operations in the native code. Natively file write will always take a longer time then file read.

破晓 2025-01-12 17:17:52

我在 Android 的源代码中看到,对于大型 sql 查询,使用了 applyBatch 方法。在ContactsRemover等其他一些程序中,作者也使用了这种方法。但我不知道它是否真的比 insert 或 bultInsert 更快。

I've saw in the sources of Android that for large sql queries applyBatch method is used. In some other programs as ContactsRemover the authors also use this method. But I do not know if it is really faster then insert or bultInsert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文