Skip to content

Instantly share code, notes, and snippets.

@tophyr
Last active August 29, 2015 14:06
Show Gist options
  • Save tophyr/665be244b4f8e7b2961e to your computer and use it in GitHub Desktop.
Save tophyr/665be244b4f8e7b2961e to your computer and use it in GitHub Desktop.
09-10 00:46:57.210 19553-19566/sarbs.com.tinker I/Tinker﹕ Running test...
09-10 00:47:25.250 19553-19566/sarbs.com.tinker I/Tinker﹕ Full loop with branching took 27535510 µs.
09-10 00:48:57.268 19553-19566/sarbs.com.tinker I/Tinker﹕ Full loop with virtmethod took 91837723 µs.
package sarbs.com.tinker;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
public class Tinker extends Activity {
// don't make these private, in order to avoid synthetic accessors
int t;
int bogus = 0;
private interface Test {
Test crunch();
}
private class One implements Test {
@Override
public Test crunch() {
bogus++;
bogus--;
return (++t > 10 ? new Two() : this);
}
}
private class Two implements Test {
@Override
public Test crunch() {
bogus++;
bogus--;
return this;
}
}
public final void runTest() {
long start, end;
boolean s = false;
t = 0;
start = SystemClock.currentThreadTimeMicro();
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if (s) {
bogus++;
bogus--;
s = (++t > 10);
} else {
bogus++;
bogus--;
}
}
end = SystemClock.currentThreadTimeMicro();
Log.i("Tinker", String.format("Full loop with branching took %d µs.", end - start));
t = 0;
Test v = new One();
start = SystemClock.currentThreadTimeMicro();
for (int i = 0; i < Integer.MAX_VALUE; i++) {
v = v.crunch();
}
end = SystemClock.currentThreadTimeMicro();
Log.i("Tinker", String.format("Full loop with virtmethod took %d µs.", end - start));
}
@Override
protected void onStart() {
super.onStart();
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
Log.i("Tinker", "Running test...");
runTest();
return null;
}
}.execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment