Parcelable和 Serializable 序列化時間測試

Parcelable和 Serializable 序列化時間測試

測試硬件
Android虛擬機
androId9.0
CPU/ABI: Google Play Intel Atom (x86)

測試代碼
public class TextA implements Parcelable {
private String message;
private long time;

public TextA(String message, long time) {
    this.message = message;
    this.time = time;
}

protected TextA(Parcel in) {
    message = in.readString();
    time = in.readLong();
}

public static final Creator<TextA> CREATOR = new Creator<TextA>() {
    @Override
    public TextA createFromParcel(Parcel in) {
        return new TextA(in);
    }

    @Override
    public TextA[] newArray(int size) {
        return new TextA[size];
    }
};

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(message);
    dest.writeLong(time);
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public long getTime() {
    return time;
}

public void setTime(long time) {
    this.time = time;
}

}

public class TextB implements Serializable {
private String message;
private long time;

public TextB(String message, long time) {
    this.message = message;
    this.time = time;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public long getTime() {
    return time;
}

public void setTime(long time) {
    this.time = time;
}

}

關鍵測試代碼
public void onClickBtnP(View view) {
long start = System.currentTimeMillis();
TextA();
LogUtils.e("Parcelable time = " + (System.currentTimeMillis() - start));
}

public void onClickBtnS(View view) {

// TextB testA = new TextB(“Serializable”, System.currentTimeMillis());
// IntentUtils.startActivity(this, TestActivity.class, testA);
long start = System.currentTimeMillis();
TextB();
LogUtils.e("Serializable time = " + (System.currentTimeMillis() - start));
}

private void TextA() {
    TextA testA = new TextA("Parcelable", System.currentTimeMillis());
    Parcel parcel = Parcel.obtain();
    long start = System.currentTimeMillis();
    for (int i = 0; i < 1100; i++) {
        testA.writeToParcel(parcel, 0);
    }
    LogUtils.e("序列化 - Parcelable time = " + (System.currentTimeMillis() - start));
    parcel.setDataPosition(0);
    long start2 = System.currentTimeMillis();
    for (int i = 0; i < 1100; i++) {
        TextA.CREATOR.createFromParcel(parcel);
    }
    LogUtils.e("反序列化 Parcelable time = " + (System.currentTimeMillis() - start2));
    parcel.recycle();
}

private void TextB() {
    TextB testA = new TextB("Serializable", System.currentTimeMillis());
    ObjectOutputStream out = null;
    ByteArrayOutputStream bas = new ByteArrayOutputStream(1_000_000);

    try {
        out = new ObjectOutputStream(bas);
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1100; i++) {
            out.writeObject(testA);
        }
        LogUtils.e("序列化 Serializable time = " + (System.currentTimeMillis() - start));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    byte[] byteArray = bas.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
    ObjectInputStream in = null;

    try {
        in = new ObjectInputStream(bis);
        long start2 = System.currentTimeMillis();
        for (int i = 0; i < 1100; i++) {
            in.readObject();
        }
        LogUtils.e("反序列化 Serializable time = " + (System.currentTimeMillis() - start2));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

測試日誌打印
結果分析:Parcelable 序列化平均時間爲:260(ms)
Parcelable 反序列化平均時間爲:365.3125(ms)
Serializable 序列化平均時間爲:977.6875(ms)
Serializable 反序列化平均時間爲:1066.625(ms)

    Serializable序列化耗時約是Parcelable3.8倍
    Serializable反序列化耗時約是Parcelable2.9倍

P 序列化 P 反序列化 S 序列化 S 反序列化
257 416 1558 876
252 380 801 1296
253 366 693 921
284 287 686 963
270 361 705 911
362 527 703 895
241 380 731 923
251 385 1293 1278
245 382 1510 865
252 275 865 936
248 376 1374 1808
251 281 1254 1045
245 371 719 975
250 355 967 1544
248 355 1060 905
251 348 724 925

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章