Android中的序列化性能

时间:2018-12-14 22:56:37

标签: java android

我尝试比较Serializable,Externalizable和Parcelable。在Android中可外部化的速度比可序列化的速度快吗?

我希望Externalizable比Serializable快得多,但实际上,我看到的结果完全相同。

我测试的想法:创建许多活动并使用方法Bundle getSerializable获取Serializables或Externalizables对象的列表。并比较性能。

我的测试结果:

1) 可外部化:3462毫秒 可序列化:3762毫秒 Parceble:300毫秒

2) 可外部化:3780毫秒 可序列化:3853毫秒 Parceble:329毫秒

3)

可外部扩展:3533毫秒 可序列化:4063毫秒 Parceble:339毫秒

此处的代码:https://github.com/mlevytskiy/SerializableVsParcebleVsExternalizable

package serializable.petproject.serializablevsparceblevsexternalizable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;


public class MySerializableClass implements Serializable {

    public static final long serialVersionUID = 1865151003590118998L;
    public String mStringData = "";
    public int mIdenty = 0;
    public boolean mIsActive = false;
    public float mDest = 0.0f;
    public ArrayList<String> mStringList;

    public MySerializableClass() {
        mStringData = UUID.randomUUID().toString();

        mStringList = new ArrayList<String>();
        Random rand = new Random();
        int maxI = rand.nextInt();

        for (int i = 0; i < maxI % 15; i++) 
        {
            mStringList.add(UUID.randomUUID().toString());
        }

        mIdenty = rand.nextInt();
        mIsActive = rand.nextBoolean();
        mDest = rand.nextFloat();
    }
}

可外部化:

    package serializable.petproject.serializablevsparceblevsexternalizable;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;

/**
 * Created by max on 12/12/18.
 */

public class MyExternalizbleClass implements Externalizable {

    public static final long serialVersionUID = 1865151003590118998L;
    public String mStringData = "";
    public int mIdenty = 0;
    public boolean mIsActive = false;
    public float mDest = 0.0f;
    public ArrayList<String> mStringList;

    public MyExternalizbleClass() {
        mStringData = UUID.randomUUID().toString();

        mStringList = new ArrayList<String>();
        Random rand = new Random();
        int maxI = rand.nextInt();

        for (int i = 0; i < maxI % 15; i++)
        {
            mStringList.add(UUID.randomUUID().toString());
        }

        mIdenty = rand.nextInt();
        mIsActive = rand.nextBoolean();
        mDest = rand.nextFloat();
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(mStringData);
        out.writeInt(mIdenty);
        out.writeBoolean(mIsActive);
        out.writeFloat(mDest);
        out.writeObject(mStringList);

    }

    @Override
    public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
        mStringData = input.readUTF();
        mIdenty = input.readInt();
        mIsActive = input.readBoolean();
        mDest = input.readFloat();
        mStringList = (ArrayList<String>) input.readObject();
    }

}

活动:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by max on 12/14/18.
 */

public class ExternalizableActivity extends Activity {
    public static final String PARCELABLE_ARRAY = "parcelable_array";

    public ArrayList<MyExternalizbleClass> items;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Date time = new Date();
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            items = (ArrayList<MyExternalizbleClass>) extras.getSerializable(PARCELABLE_ARRAY);
        }
        Date now = new Date();
        long delta = now.getTime() - time.getTime();

        if (App.attendCount == 0) {
            Log.w("MainActivity", "Summary externalizable time: " + App.allTime + " ms.");
            //clear
            App.allTime = -1;
            App.attendCount = App.ITEMS_COUNT;
            return;
        }

        if (App.attendCount > 0) {
            App.allTime = App.allTime + delta;
            App.attendCount = App.attendCount - 1;
            startTest();
        }
    }

    public void startTest() {
        items = new ArrayList<>();
        for (int i = 0; i < App.ITEMS_COUNT; i++) {
            items.add(new MyExternalizbleClass());
        }
        Intent i = new Intent(getApplicationContext(), ExternalizableActivity.class);
        i.putExtra(PARCELABLE_ARRAY, items);
        startActivity(i);
        finish();
    }

}

0 个答案:

没有答案