這一開始寫分享, 想寫的東西是愈來愈多, 不少以前看過可是比較模糊的都想寫(手動doge, 這不, 今天看到個項目用了Parcelable, 就想着看看這倆序列化方式html
序列化不知道你們都用在哪裏, 不考慮進程間通訊, 好像只在Intent.putExtra()裏用到, 那麼爲啥有了Serializable還要搞個Parcelable呢? 這倆有啥不同呢?java
這個是標準Java中提供的序列化方法, 優勢沒別的, 簡單好用, 這裏的好用, 不是說性能, 而是使用感覺= =. 使用起來很簡單, 直接實現Serializable接口便可. 超級無腦android
給個Google Reference裏的使用方法, 主要是要提供一個叫CREATOR非空的靜態字段而且這個字段實現Parcelable.Creator類, 這裏好繞啊, 不過有官方示例:git
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
複製代碼
到這裏是否是有人以爲就能夠結束了? Naive! (手動推眼鏡github
咳咳, 以上都沒啥問題, 可是並不許確, 由於Serializable不單單隻提供了自動序列化方式, 因此像Parcelable同樣, 能夠自行指定序列化的方式:app
private void writeObject(java.io.ObjectOutputStream out) throws IOException;
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;
複製代碼
這個是引用的StackOverflow的提問 :ide
Usual Java serialization on an average Android device (if done right *) is about 3.6 times faster than Parcelable for writes and about 1.6 times faster for reads. Also it proves that Java Serialization (if done right) is fast storage mechanism that gives acceptable results even with relatively large object graphs of 11000 objects with 10 fields each. * The sidenote is that usually everybody who blindly states that "Parcelable is mush faster" compares it to default automatic serialization, which uses much reflection inside. This is unfair comparison, because Parcelable uses manual (and very complicated) procedure of writing data to the stream. What is usually not mentioned is that standard Java Serializable according to the docs can also be done in a manual way, using writeObject() and readObject() methods. For more info see JavaDocs. This is how it should be done for the best performance.性能
大概是說Serializable速度很快, 在平均水平的Android機器上比Parcelable讀取快1.6倍, 寫入快3.6倍...Parcelable比Serializable快, 是由於對比的是自動序列化的Serializable...測試
徹底跟以前的說法反着了啊...this
還有個說法是這樣的
Parcelable不能使用在要將數據存儲在磁盤上的狀況,由於Parcelable不能很好的保證數據的持續性在外界有變化的狀況下。儘管Serializable效率低點, 也不提倡用,但在這種狀況下,仍是建議你用Serializable 。
來源是 這個連接 11年寫的, 已是我找到的最先的了, 不知道緣由是啥, 有懂的老哥能夠指教交流一下
借這位大佬的代碼一用
能夠看到Serializable設置了readObject和writeObject以後就很快了, 並且傳輸的體積要小.So, if serializable is faster and easier to implement, why android has parcelable at all? The reason is native code. Parcelable is created not just for interprocess communication. It also can be used for intercode communication. You can send and recieve objects from C++ native layer. That's it.
What should you choose? Both will work good. But I think that Parcelable is better choice since it is recommended by google and as you can see from this thread is much more appreciated.
因此各位之後仍是看需求使用這倆吧, 至少幹掉自動序列化, 一點兒性能都不能少(手動Doge