The difference between parcelable and serializable in Android systems

Source: Internet
Author: User

When it comes to Android development, we all know we can't pass references to activities or fragments, we need to put them in a intent or bundle, and then pass them on.

Through the Android API, we know that there are two options, that is, when passing objects, we need to parcelable or serializable our objects. As a Java developer, I believe you have a certain understanding of serializable mechanism, then why do you need parcelable?

To answer this question, let's look at the difference between the two separately.

Serializable, easy to use

public class Serializabledeveloper implements Serializable

String name;

int yearsofexperience;

List<skill> skillset;

float favoritefloat;

Static Class Skill implements Serializable {

String name;

Boolean programmingrelated;

}

}

The beauty of serializable is that you only need to implement serializable interfaces for a class and its properties. The Serializable interface is an identity interface (marker interface), which means that Java will efficiently serialize the object without implementing a method.

The disadvantage of this approach is that reflection is used, and the process of serialization is slow. This mechanism creates many temporary objects at serialization time and easily triggers garbage collection.

Parcelable, Speed first

Access modifiers, accessors and regular constructors ommited for brevity

Class Parcelabledeveloper implements Parcelable {

String name;

int yearsofexperience;

List<skill> skillset;

float favoritefloat;

Parcelabledeveloper (Parcel in) {

THIS.name = In.readstring ();

This.yearsofexperience = In.readint ();

This.skillset = new arraylist<skill> ();

In.readtypedlist (skillset, skill.creator);

This.favoritefloat = In.readfloat ();

}

void Writetoparcel (Parcel dest, int flags) {

Dest.writestring (name);

Dest.writeint (yearsofexperience);

Dest.writetypedlist (skillset);

Dest.writefloat (favoritefloat);

}

int describecontents () {

return 0;

}

Static final parcelable.creator<parcelabledeveloper> Creator

= new Parcelable.creator<parcelabledeveloper> () {

Parcelabledeveloper Createfromparcel (Parcel in) {

return new Parcelabledeveloper (in);

}

Parcelabledeveloper[] NewArray (int size) {

return new Parcelabledeveloper[size];

}

};

Static Class Skill implements Parcelable {

String name;

Boolean programmingrelated;

Skill (Parcel in) {

THIS.name = In.readstring ();

this.programmingrelated = (In.readint () = = 1);

}

@Override

void Writetoparcel (Parcel dest, int flags) {

Dest.writestring (name);

Dest.writeint (programmingrelated 1:0);

}

Static final parcelable.creator<skill> Creator

= new Parcelable.creator<skill> () {

Skill Createfromparcel (Parcel in) {

return new Skill (in);

}

Skill[] NewArray (int size) {

return new Skill[size];

}

};

@Override

int describecontents () {

return 0;

}

}

}

According to Google engineers, the code will run very fast. One reason is that we have a clear understanding of the process of serialization without the need to use reflection to infer it. The code of the object also needs to be highly optimized for faster serialization.

Therefore, it is obviously not easy to achieve parcelable. Implementing the Parcelable interface requires writing a lot of template code, which makes the object code difficult to read and maintain.

Speed test

Of course, we still want to know how fast parcelable is relative to serializable.

Test method

By putting an object into a bundle and then invoking the Bundle#writetoparcel (Parcel, int) method to simulate passing the object to an activity, the object is then taken out.

Run 1000 times in a loop.

The two methods run 10 times to reduce memory consolidation, and the CPU is disturbed by other application usage.

The object involved in the test is serializabledeveloper and Parcelabledeveloper in the code above.

Test on a variety of Android hardware and software environments

LG Nexus 4–android 4.2.2

Samsung Nexus 10–android 4.2.2

HTC Desire z–android 2.3.3

NEXUS 10

Serializable:1.0004ms, parcelable:0.0850ms– 10.16 times times higher.

NEXUS 4

serializable:1.8539ms–parcelable:0.1824ms– 11.8 times times higher.

DESIRE Z

Serializable:5.1224ms–parcelable:0.2938ms– 17.36 times times higher.

It can be concluded that parcelable is more than 10 times faster than serializable. Interestingly, even on a powerful hardware such as the Nexus 10, the process of serializing and deserializing a fairly simple object takes nearly a millisecond.

Summarize

If you want to be a good software engineer, you need to spend more time implementing parcelable, because it will be more than 10 times faster for your object's serialization process and takes up less resources.

But in most cases, Serializable's turtle speed is not too noticeable. You want to steal a little lazy to use it, but remember serialization is a relatively resource-intensive operation, as far as possible use less.

If you want to pass a list of many objects, the entire serialization process can take longer than a second, which can make the screen turn a little too late.

English Original: Developerphil

Source: Android Cool Posts

Links: Http://greenrobot.me/devpost/android-parcelable-serializ

The difference between parcelable and serializable in Android systems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.