最快的方法插入到的话UserDictionary [英] Fastest method to insert words into UserDictionary

查看:361
本文介绍了最快的方法插入到的话UserDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想应用到多个(2K)单词添加到UserDictionary。

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

我已经尝试了ContentResolver的()。插入/ BULK INSERT ....

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();

在我的手机,如果我做的100 3+分钟批次bulkinsert插入2K字需要每字约100毫秒。

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?

侧问题:是否有UserDictionary大小的上限或不依赖于手机

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

任何帮助AP preciated

Any help appreciated

Michealster

Michealster

推荐答案

具体到你的情况:
一气加入在字典2000字需要时间和没有太多可进行优化的。你在这里看到的时间是用于文件操作,并从Java的> NDK->本地操作移动。

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.

常规:
无论使用哪种方法,人们必须明白,这些都不算什么,但文件操作,并肯定需要时间。这些API(包括相关的SQLite)都只是一个包装在本地code预期的文件操作。本机文件写入总是会需要更长的时间,那么文件中读取。

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.

这篇关于最快的方法插入到的话UserDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆