为什么Android为序列化对象提供了两个接口?序列化对象与Android Binder和AIDL文件interopt ?


当前回答

打包比使用Binder序列化快得多,因为序列化使用反射并导致许多GC。Parcelable是为了优化传递对象而设计的。

这是参考链接。 http://www.developerphil.com/parcelable-vs-serializable/

其他回答

在编组和解组方面存在一些性能问题。Parcelable比Serializable快两倍。

请浏览以下连结:

http://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development

Parcelable是Android开发中的一种标准。但不是因为速度

Parcelable是数据传输的推荐方法。但是如果你正确地使用serializable,你会发现serializable有时甚至比parcelable还要快。或者至少时间是可以比较的。

Parcelable比Serializable快吗?

不,如果序列化操作正确的话。

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呢?

原因在于本地代码。创建Parcelable不仅仅是为了进程间通信。它也可以用于代码间通信。你可以从c++原生层发送和接收对象。就是这样。

你应该选择什么?两者都很有效。但我认为Parcelable是更好的选择,因为谷歌推荐它,你可以从这个帖子中看到,它更受欢迎。

1. 可序列化的

@see http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

什么接口?

是标准的Java接口吗

速度

比Parcelable慢

2. Parcelable

@see http://developer.android.com/reference/android/os/Parcelable.html

什么接口?

android。操作系统接口 这意味着谷歌开发的Parcelable在android上有更好的性能

速度

更快(因为它针对android开发进行了优化)

综上所述

要知道Serializable是一个标准的Java接口,Parcelable是Android开发的接口

If you want to be a good citizen, take the extra time to implement Parcelable since it will perform 10 times faster and use less resources. However, in most cases, the slowness of Serializable won’t be noticeable. Feel free to use it but remember that serialization is an expensive operation so keep it to a minimum. If you are trying to pass a list with thousands of serialized objects, it is possible that the whole process will take more than a second. It can make transitions or rotation from portrait to lanscape feel very sluggish.

来源:http://www.developerphil.com/parcelable-vs-serializable/

Serializable接口可以像Parcelable接口一样使用,从而获得(不是很多)更好的性能。 只需覆盖这两个方法来处理手动编组和解组过程:

private void writeObject(java.io.ObjectOutputStream out)
    throws IOException
private void readObject(java.io.ObjectInputStream in)
    throws IOException, ClassNotFoundException

不过,在我看来,在开发原生Android时,使用Android api是正确的选择。

看到的:

https://bitbucket.org/afrishman/androidserializationtest/ https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html