-
-
Save davebren/1249d6be4b5e21962205 to your computer and use it in GitHub Desktop.
Benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dbreneisen.testapp; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import android.util.Log; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectOutput; | |
import java.io.ObjectOutputStream; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MyActivity extends Activity { | |
private static final String LOG_TAG = MyActivity.class.getSimpleName(); | |
private static final String LOG_FORMAT = "%s\t%f"; | |
private static final String DUMMY_STRING = "string"; | |
private static final float DUMMY_FLOAT = 1234567f; | |
private static final int DUMMY_INT = 1234567; | |
private static final boolean DUMMY_BOOLEAN = false; | |
private static final int N = 1000; | |
private static final int LIST_SIZE = 25; | |
@Override protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
new Thread(new Runnable() { | |
@Override public void run() { | |
runBenchmark(new ParcelableBenchmark(), "Parcelable"); | |
runBenchmark(new SerializableBenchmark(), "Serializable"); | |
} | |
}).start(); | |
} | |
static void runBenchmark(Benchmark b, String name) { | |
Log.i(LOG_TAG, String.format(LOG_FORMAT, name, b.run(N) / 1000000)); | |
} | |
public static interface Benchmark { | |
double run(int N); | |
} | |
public static class ParcelableBenchmark implements Benchmark { | |
@Override public double run(int N) { | |
ParcelableModel p = ParcelableModel.dummyData(); | |
double average = 0; | |
double start; | |
double end; | |
int x = 0; | |
while (x < N) { | |
start = System.nanoTime(); | |
p.writeToParcel(Parcel.obtain(), 0); | |
end = System.nanoTime(); | |
average += (end - start); | |
x++; | |
} | |
return (average / N); | |
} | |
} | |
public static class SerializableBenchmark implements Benchmark { | |
@Override public double run(int N) { | |
SerializableModel p = SerializableModel.dummyData(); | |
double average = 0; | |
double start; | |
double end; | |
ByteArrayOutputStream bos = null; | |
ObjectOutput out = null; | |
int x = 0; | |
while (x < N) { | |
start = System.nanoTime(); | |
try { | |
bos = new ByteArrayOutputStream(); | |
out = new ObjectOutputStream(bos); | |
out.writeObject(p); | |
bos.toByteArray(); | |
} catch (IOException ignored) { | |
} finally { | |
try { if (out != null) { out.close(); } } catch (IOException ignored) {} | |
try { if (bos != null) { bos.close(); } } catch (IOException ignored) {} | |
} | |
end = System.nanoTime(); | |
average += (end - start); | |
x++; | |
} | |
return (average / N); | |
} | |
} | |
public static class ParcelableModel implements Parcelable { | |
public String name; | |
public int yearsOfExperience; | |
public List<Skill> skillSet; | |
public float favoriteFloat; | |
public ParcelableModel() {} | |
public ParcelableModel(Parcel in) { | |
name = in.readString(); | |
yearsOfExperience = in.readInt(); | |
skillSet = new ArrayList<Skill>(); | |
in.readTypedList(skillSet, Skill.CREATOR); | |
favoriteFloat = in.readFloat(); | |
} | |
@Override public void writeToParcel(Parcel dest, int flags) { | |
dest.writeString(name); | |
dest.writeInt(yearsOfExperience); | |
dest.writeTypedList(skillSet); | |
dest.writeFloat(favoriteFloat); | |
} | |
@Override public int describeContents() { | |
return 0; | |
} | |
static final Creator<ParcelableModel> CREATOR = | |
new Creator<ParcelableModel>() { | |
@Override public ParcelableModel createFromParcel(Parcel in) { | |
return new ParcelableModel(in); | |
} | |
@Override public ParcelableModel[] newArray(int size) { | |
return new ParcelableModel[size]; | |
} | |
}; | |
static class Skill implements Parcelable { | |
public String name; | |
public boolean programmingRelated; | |
public Skill() {} | |
public Skill(Parcel in) { | |
name = in.readString(); | |
programmingRelated = (in.readInt() == 1); | |
} | |
@Override public void writeToParcel(Parcel dest, int flags) { | |
dest.writeString(name); | |
dest.writeInt(programmingRelated ? 1 : 0); | |
} | |
@Override public int describeContents() { | |
return 0; | |
} | |
static final Creator<Skill> CREATOR = new Creator<Skill>() { | |
@Override public Skill createFromParcel(Parcel in) { | |
return new Skill(in); | |
} | |
@Override public Skill[] newArray(int size) { | |
return new Skill[size]; | |
} | |
}; | |
} | |
public static ParcelableModel dummyData() { | |
ParcelableModel p = new ParcelableModel(); | |
p.name = DUMMY_STRING; | |
p.yearsOfExperience = DUMMY_INT; | |
p.skillSet = new ArrayList<Skill>(LIST_SIZE); | |
p.favoriteFloat = DUMMY_FLOAT; | |
for (int x = 0; x < LIST_SIZE; x++) { | |
Skill s = new Skill(); | |
s.programmingRelated = DUMMY_BOOLEAN; | |
s.name = DUMMY_STRING; | |
p.skillSet.add(s); | |
} | |
return p; | |
} | |
} | |
public static class SerializableModel implements Serializable { | |
public String name; | |
public int yearsOfExperience; | |
public List<Skill> skillSet; | |
public float favoriteFloat; | |
public static class Skill implements Serializable { | |
public String name; | |
public boolean programmingRelated; | |
} | |
public static SerializableModel dummyData() { | |
SerializableModel p = new SerializableModel(); | |
p.name = DUMMY_STRING; | |
p.yearsOfExperience = DUMMY_INT; | |
p.skillSet = new ArrayList<Skill>(LIST_SIZE); | |
p.favoriteFloat = DUMMY_FLOAT; | |
for (int x = 0; x < LIST_SIZE; x++) { | |
Skill s = new Skill(); | |
s.programmingRelated = DUMMY_BOOLEAN; | |
s.name = DUMMY_STRING; | |
p.skillSet.add(s); | |
} | |
return p; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment