狠狠撸

狠狠撸Share a Scribd company logo
ORM各library效能分析與使用
(硬湊的主題) https://goo.gl/iLOCYN
Takuma
ORM Library
1. ormlite
2. greendao
3. realm
4. DBFlow
5. ActiveAndroid
ORM Library
1. ormlite
2. greendao
3. realm
4. DBFlow
5. ActiveAndroid
何謂ORM?
Object Relational Mapping
何謂ORM?
Object Relational Mapping
Object Role Modeling
何謂ORM?
Object Relational Mapping
Object Role Modeling
Optical Remote Module
何謂ORM?
Object Relational Mapping
Object Role Modeling
Optical Remote Module
……
何謂ORM?
Object Relational Mapping
Object Role Modeling
Optical Remote Module
……
何謂ORM?
Object Relational Mapping
Object Role Modeling
Optical Remote Module
……
总之他就是个顿补迟补产补蝉别概念!!!
进入正题
就算不用ORM library
也沒關係
我們還有原生的SQLite!
@Override
public void onCreate(SQLiteDatabase db) {
String DATABASE_CREATE_TABLE =
"create table newMemorandum("
+ "_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
+ "date VARCHAR,"
+ "note VARCHAR,"
+ "pw VARCHAR,"
+ "reminder INT,"
+ "type VARCHAR,"
+ "memo VARCHAR"
+ ")";
db.execSQL(DATABASE_CREATE_TABLE);
}
SQLite原生寫法
我很懒!=懒是工程师的美德
我很懒=懒是工程师的美德
我很懶 == 懶是工程師的美德
@Override
public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
try {
TableUtils.createTableIfNotExists(connectionSource, Character.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
ormlite寫法
public static void main(String args[]) throws Exception {
Schema schema = new Schema(3, "com.takumalee.ormcomparison.database.greendao.dao");
createTable(schema);
generateDaoFiles(schema);
}
private static void createTable(Schema schema){
//一個Entity 對應一個 DB table
Entity point = schema.addEntity("Character");
//add table column
point.addIdProperty();
point.addStringProperty("careers").notNull();
point.addStringProperty("attributes").notNull();
}
GreenDao寫法(一)
GreenDao寫法(二)
private static void generateDaoFiles(Schema schema){
try {
DaoGenerator generator = new DaoGenerator();
//建立到指定目錄
generator.generateAll(schema, "../ORMComparison/app/src/main/java/");
} catch (Exception e){
e.printStackTrace();
}
}
……
Realm寫法
事件起源
搁别补濒尘官方表示
有效能就是任性!
再跟我大声说一次:有效能就是......
翱谤尘各濒颈产谤补谤测效能分析与使用
ormlite vs greenDao
GreenDao is much faster than ORMLite. It is roughly:
●3X faster at writing large numbers of objects.
●50% faster at reading in all 10000 entries in a single table.
●2X to 3X faster at an indexed read of a single row (though both were very fast).
●15X faster at doing a LIKE search for 100 records in a 10000 entry table.
奥罢贵?
Google Play(https://goo.gl/tnirTh)
Github(https://goo.gl/RJqYu4)
Blog(http://goo.gl/IWR5Ih)
偷渡自己的記帳App:MMM
(https://goo.gl/Lg5ZdX)
ORM 效能分析實作
Insert(錯誤條件?IfNotExist)
ormlite:
- 45.9sec per 100k
greeDAO:
- 7.3sec per 100k
realm:
- 18.5sec per 100k
Insert(ormlite拿掉IfNotExist,realm拿掉Primary key)
ormlite:
- 14.1sec per 100k
greeDAO:
- 7.1sec per 100k
realm:
- 8.6sec per 100k
Query(多做了Data Access Object)
ormlite:
- 20.3sec per 100k
greeDAO:
- 11.2sec per 100k
realm:
- 287ms per 100k
Emulator Demo
Mobile Demo
ありがとう
Facebook社群

More Related Content

翱谤尘各濒颈产谤补谤测效能分析与使用