Realm Encrypted导致Android中插入速度非常慢

时间:2018-05-07 04:02:41

标签: java android realm

我在Android中测试领域的性能。 如果我在非加密领域插入10000个数据,它的成本为16184毫秒,而加密的领域则需要36000毫秒。它太慢了! 加密会导致这种情况吗?如何解决这个问题?

SQLite代码:

            ArrayList<ContentValues> valueArray = new ArrayList<>();
        for (int i = 0; i < number; ++i) {
            ContentValues value = new ContentValues();
            value.put(PersonInfoTableEntry.COLUMN_NAME_DISPLAYINFO, list.get(i).getDisplayInfo());
            value.put(PersonInfoTableEntry.COLUMN_NAME_DISPLAYPIC, list.get(i).getDisplayPic());
            value.put(PersonInfoTableEntry.COLUMN_NAME_DISPLAYVERSION, list.get(i).getDisplayVersion());
            value.put(PersonInfoTableEntry.COLUMN_NAME_FEATURE, list.get(i).getFaceFeatures());
            value.put(PersonInfoTableEntry.COLUMN_NAME_FEATUREVERSION, list.get(i).getFeatureVersion());
            value.put(PersonInfoTableEntry.COLUMN_NAME_PEOPLEVERSION, list.get(i).getPeopleVersion());
            value.put(PersonInfoTableEntry.COLUMN_NAME_TIMESTAMPS, list.get(i).getTimestamp());
            value.put(PersonInfoTableEntry.COLUMN_NAME_META, list.get(i).getMeta());
            valueArray.add(value);
        }

        long startTime = System.currentTimeMillis();
        Log.e(TAG, "StartTime Batch: " + "[" + number + "] " + startTime);
        db.beginTransaction();
        for (ContentValues contentValue: valueArray) {
            long newRowId = db.insert(PersonInfoTableEntry.TABLE_NAME, null, contentValue);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        long endTime = System.currentTimeMillis();
        Log.e(TAG, "EndTime Batch: " + "[" + number + "] " + endTime);
        String str = "BatchWriteWithoutEncrypt [" + number + "] cost time : " + (endTime - startTime) + " ms";
        Log.e(TAG, str);

领域代码:

        ArrayList<PersonInfo> list = new ArrayList<PersonInfo>();
        Random rand = new Random();
        for (int i = 0; i < number; ++i) {
            PersonInfo tmpPersonInfo = new PersonInfo();
            tmpPersonInfo.setDisplayInfo("测试" + rand.nextInt());
            tmpPersonInfo.setDisplayPic(disPic + rand.nextInt());
            RealmList<String> tmpStrList = new RealmList<>();
            tmpStrList.add(featureStr);
            tmpPersonInfo.setFaceFeatures(tmpStrList);
            tmpPersonInfo.setDisplayVersion(md5);
            tmpPersonInfo.setFeatureVersion(md5);
            tmpPersonInfo.setMeta(md5);
            list.add(tmpPersonInfo);
        }
        Log.e(TAG,"Array size is " + list.size());

        long startTime = System.currentTimeMillis();
        Log.e(TAG, "StartTime Batch: " + "[" + number + "] " + startTime);
        realm.beginTransaction();
        realm.insert(list);
        realm.commitTransaction();

        long endTime = System.currentTimeMillis();
        Log.e(TAG, "EndTime Batch: " + "[" + number + "] " + endTime);
        String str = "BatchWriteWithoutEncrypt [" + number + "] cost time : " + (endTime - startTime) + " ms";
        Log.e(TAG, str);

1 个答案:

答案 0 :(得分:0)

替换以下代码

 long startTime = System.currentTimeMillis();
    Log.e(TAG, "StartTime Batch: " + "[" + number + "] " + startTime);
    realm.beginTransaction();
    realm.insert(list);
    realm.commitTransaction();

用这个

try(Realm realm = Realm.getDefaultInstance()){
        realm.executeTransaction( rlm -> rlm.insert(list));
        // OR i'd suggest to go for async transaction
        realm.executeTransactionAsync( rlm -> rlm.insert(list));
}