of Gson and Realm with an extremely simple object model. The overall result of the test shows that Realm performs a little faster than Gson. However, it could show different results in other test conditions. Refer to the appendix for further information. Gson.toJson Gson.fromJson Realm.createObject copyToRealm Milisecond 0 300 600 900 1200
private boolean active; } // Many to One public class Contact extends RealmObject { private Email email; } // Many to Many public class Contact extends RealmObject { private RealmList<Email> emails; }
RecyclerViewAdapter(Realm realm) { realmResults = realm.where(Post.class).findAll(); } @Override public int getItemCount() { return realmResults.size(); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { Post post = realmResults.get(position); // do something... } }
long execute(Realm realm, long version) { // Migrate from version 0 to version 1 if (version == 0) { Table personTable = realm.getTable(Person.class); long fistNameIndex = getIndexForProperty(personTable, "firstName"); long lastNameIndex = getIndexForProperty(personTable, "lastName"); long fullNameIndex = personTable.addColumn(ColumnType.STRING, "fullName"); personTable.removeColumn(getIndexForProperty(personTable, "firstName")); personTable.removeColumn(getIndexForProperty(personTable, "lastName")); version++; } return version; } } https://github.com/realm/realm-java/blob/master/examples/migrationExample
RecyclerViewAdapter(Realm realm) { realmResults = realm.where(Post.class).findAll(); } @Override public int getItemCount() { return realmResults.size(); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { Post post = realmResults.get(position); // do something... } }
{ public String address; public String number; public boolean active; } 10000 times iteration per each test. 5 times test per each method Test Code: https://github.com/Test-Codes/Realm-Java-Benchmark