10k Sqlite插入速度比Realm快

时间:2017-09-08 16:33:23

标签: android sqlite realm

我看到多个图表显示Realm数据库中的插入大约比Sqlite快两倍。但是当我自己尝试时,SQLite被证明比Realm快一点。对于我的示例代码,10k插入对于SQLite需要~1500ms,对于Realm需要~2200ms。难道我做错了什么?我怎样才能加速Realm?提前谢谢。

long startTime = Calendar.getInstance().getTimeInMillis();

    realm.beginTransaction();

    for(int i = 1; i < 10001; i++) {

        final Employee employee = new Employee();
        employee.setId(i);
        employee.setFirstName("John");
        employee.setLastName("Smith");
        employee.setAge(30);
        employee.setSalary(3000);

        realm.insert(employee);
    }
    realm.commitTransaction();

    long endTime = Calendar.getInstance().getTimeInMillis();
    long result = endTime - startTime;

SQLite的

long timestart = Calendar.getInstance().getTimeInMillis();

db.beginTransaction();

for(int i = 0; i < 10000; i++) {
        Employee employee = new Employee();
        employee.setFirstName("John");
        employee.setLastName("Smith");
        employee.setAge(30);
        employee.setSalary(3000);

    ContentValues contentValues = new ContentValues();
    contentValues.put(DatabaseContract.EmployeeEntry.COLUMN_FIRST_NAME, employee.getFirstName());
    contentValues.put(DatabaseContract.EmployeeEntry.COLUMN_LAST_NAME, employee.getLastName());
    contentValues.put(DatabaseContract.EmployeeEntry.COLUMN_AGE, employee.getAge());
    contentValues.put(DatabaseContract.EmployeeEntry.COLUMN_SALARY, employee.getSalary());

    long rowId = db.insert(DatabaseContract.EmployeeEntry.TABLE_NAME, null, contentValues);

}

db.setTransactionSuccessful();
db.endTransaction();

long timeEnd = Calendar.getInstance().getTimeInMillis();
long result = timeEnd - timestart;

0 个答案:

没有答案