转载

别再说Serializable性能不如Parcelable啦

这一开始写分享, 想写的东西是越来越多, 很多之前看过但是比较模糊的都想写(手动doge, 这不, 今天看到个项目用了Parcelable, 就想着看看这俩序列化方式

引入

序列化不知道大家都用在哪里, 不考虑进程间通信, 好像只在Intent.putExtra()里用到, 那么为啥有了Serializable还要搞个Parcelable呢? 这俩有啥不一样呢?

先说使用

Serializable

这个是标准Java中提供的序列化方法, 优点没别的, 简单好用, 这里的好用, 不是说性能, 而是使用感受= =. 使用起来很简单, 直接实现Serializable接口即可. 超级无脑

Parcelable

给个Google Reference里的使用方法, 主要是要提供一个叫CREATOR非空的静态字段并且这个字段实现Parcelable.Creator类, 这里好绕啊, 不过有官方示例:

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();
    }
}
复制代码

区别

  1. Parcelable在内存中的序列化效率比Serializable快
  2. Serializable在序列化过程中使用反射实现, 并且会生成大量的临时对象

到这里是不是有人觉得就可以结束了? Naive! (手动推眼镜

但是

咳咳, 以上都没啥问题, 但是并不准确, 因为Serializable不仅仅只提供了自动序列化方式, 所以像Parcelable一样, 可以自行指定序列化的方式:

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的提问 :

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...

完全跟之前的说法反着了啊...

存疑

还有个说法是这样的

Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。

来源是这个链接 11年写的, 已经是我找到的最早的了, 不知道原因是啥, 有懂的老哥可以指教交流一下

总结

借这位大佬的 代码 一用

别再说Serializable性能不如Parcelable啦

可以看到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

原文  https://juejin.im/post/5c652801518825627e2f9164
正文到此结束
Loading...