Always Parcelable, never Serializable (in Android)

“The final conclusion of Android Team is that Serializable in JAVA is far too slow to satisfy Android’s interprocess-communication requirements. So the team built the Parcelable solution”. Just read this sentence is enough to forget Serializable mechanism. But, I want to show you how the faster is Parcelable in comparison with Serializable. We are going to measure the time it takes to pass a structure in Android using the intent when we start a new activity.

We have around 340 nodes into ArrayList structure. Each node is a simple class that contains two String attributes and one Double attribute.

1
2
3
4
5
public class Foo  {  //implements Parcelable or Serializable
	private String mVar1;
	private Double mVar2
	private String mVar3;
}

This is the way how we pass the date if the structure is Serializable

1
2
3
4
5
6
7
8
9
// Code of Activity1
ArrayList array = fillArray();
Intent popupIntent = new Intent(this, Activity2.class);
popupIntent.putExtra("key_array", array);
startActivity(popupIntent);
 
// Code of Activity2
Intent i = getIntent();
ArrayList array = i.getSerializableExtra("key_array"));

And this is the way how we pass the date if the structure is Parcelable

1
2
3
4
5
6
7
8
9
//Code of Activity1
ArrayList array = fillArray();
Intent popupIntent = new Intent(this, Activity2.class);
popupIntent.putParcelableArrayListExtra("key_array", array);
startActivity(popupIntent);
 
//Code of Activity2
Intent i = getIntent();
ArrayList array = i.getParcelableArrayListExtra("key_array");

Finally, you can see the times of this experiment. I’ve measured the time it takes to pass the structure in both cases (Serializable and Parcelable). The results are overwhelming.

Elements Serializable Time(ms) Parcelable Time(ms)
340 3134 231
332 2708 157
342 3360 159
341 3151 186

We can conclude that Parcelable is around 15 times faster than Serializable!!

Implement parcelable structure requires a little effort because is needed implement two methods for the serialization of the object, but it’s highly recommended do it.

facebooktwittergoogle_pluslinkedinmailby feather
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>