diff --git a/app/src/main/java/com/performance/ua/performancelab/ContainerActivity.java b/app/src/main/java/com/performance/ua/performancelab/ContainerActivity.java index d46c89c..b7a046a 100644 --- a/app/src/main/java/com/performance/ua/performancelab/ContainerActivity.java +++ b/app/src/main/java/com/performance/ua/performancelab/ContainerActivity.java @@ -3,15 +3,18 @@ import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; +import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Trace; import android.support.v7.app.AppCompatActivity; +import android.util.ArrayMap; import android.util.Log; import android.view.View; import android.webkit.WebView; import java.util.Arrays; +import java.util.HashMap; import java.util.Random; /** @@ -29,7 +32,7 @@ protected void onCreate(Bundle savedInstanceState) { findViewById(R.id.container_progress).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - dumpPopularRandomNumbersByRank(); + mTask = new MyTask().execute(); } }); WebView webView = (WebView) findViewById(R.id.anim_view); @@ -64,6 +67,33 @@ public void dumpPopularRandomNumbersByRank() { Trace.endSection(); } + @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) + public void dumpPopularRandomNumbersByRankOptimised() { + Trace.beginSection("Data Structures"); + // First we need a sorted list of the numbers to iterate through. + ArrayMap sortedNumbers = new ArrayMap<>(); + sortedNumbers.putAll(coolestRandomNumbersMap); + coolestRandomNumbers.clone(); + Integer[] sorted = {}; + sorted = sortedNumbers.keySet().toArray(sorted); + Arrays.sort(sorted); + + // Great! Now because we have no rank lookup in the population-sorted array, + // take the random number in sorted order, and find its index in the array + // that's sorted by popularity. The index is the rank, so report that. Easy and efficient! + // Except that it's... you know... It's not. + for (int i = 0; i < sorted.length; i++) { + Integer currentNumber = sorted[i]; + for (int j = 0; j < coolestRandomNumbers.length; j++) { + if (currentNumber.compareTo(coolestRandomNumbers[j]) == 0) { + Log.i("Popularity Dump", currentNumber + ": #" + j); + } + } + } + Trace.endSection(); + } + + public static HashMap coolestRandomNumbersMap = new HashMap<>(); public static Integer[] coolestRandomNumbers = new Integer[3000]; static int temp; @@ -72,6 +102,28 @@ public void dumpPopularRandomNumbersByRank() { for (int i = 0; i < 3000; i++) { temp = randomGenerator.nextInt(); coolestRandomNumbers[i] = temp; + coolestRandomNumbersMap.put(temp, i); } } + + private AsyncTask mTask; + + + class MyTask extends AsyncTask { + + @Override + protected Void doInBackground(Void... params) { + dumpPopularRandomNumbersByRankOptimised(); + return null; + } + + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (mTask != null && + mTask.getStatus() == AsyncTask.Status.RUNNING) + mTask.cancel(true); + } } diff --git a/app/src/main/java/com/performance/ua/performancelab/FibActivity.java b/app/src/main/java/com/performance/ua/performancelab/FibActivity.java index 893657d..1940f9f 100644 --- a/app/src/main/java/com/performance/ua/performancelab/FibActivity.java +++ b/app/src/main/java/com/performance/ua/performancelab/FibActivity.java @@ -2,6 +2,7 @@ import android.content.Context; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; @@ -22,6 +23,8 @@ public static void start(Context context) { context.startActivity(new Intent(context, FibActivity.class)); } + private AsyncTask mTask; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -30,7 +33,7 @@ protected void onCreate(Bundle savedInstanceState) { findViewById(R.id.fib_progress).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - textView.setText(String.valueOf(computeFibonacci(POSITION_IN_FIB_SEQUENCE))); + mTask = new MyTask().execute(); } }); WebView webView = (WebView) findViewById(R.id.anim_view); @@ -49,4 +52,27 @@ public int computeFibonacci(int positionInFibSequence) { } } + + class MyTask extends AsyncTask { + + @Override + protected String doInBackground(Void... params) { + return String.valueOf(computeFibonacci(POSITION_IN_FIB_SEQUENCE)); + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + textView.setText(s); + } + } + + + @Override + protected void onDestroy() { + super.onDestroy(); + if (mTask != null && + mTask.getStatus() == AsyncTask.Status.RUNNING) + mTask.cancel(true); + } } diff --git a/app/src/main/java/com/performance/ua/performancelab/MemoryChurnActivity.java b/app/src/main/java/com/performance/ua/performancelab/MemoryChurnActivity.java index d4fef0b..006289c 100644 --- a/app/src/main/java/com/performance/ua/performancelab/MemoryChurnActivity.java +++ b/app/src/main/java/com/performance/ua/performancelab/MemoryChurnActivity.java @@ -49,18 +49,17 @@ public void imPrettySureSortingIsFree() { } } - // Now go through and dump the sorted version of each row to output! for (int i = 0; i < lotsOfInts.length; i++) { - String rowAsStr = ""; + StringBuilder sb = new StringBuilder(); for (int j = 0; j < lotsOfInts[i].length; j++) { // Clearly, the only reasonable way to construct a string is one character at a // time, with lots and lots of convenient concatenation. - rowAsStr += getSorted(lotsOfInts[i])[j]; + sb.append(getSorted(lotsOfInts[i])[j]); if (j < (lotsOfInts[i].length - 1)) { - rowAsStr += ", "; + sb.append(", "); } } - Log.i("MemoryChurnActivity", "Row " + i + ": " + rowAsStr); + Log.i("MemoryChurnActivity", "Row " + i + ": " + sb.toString()); } } diff --git a/app/src/main/java/com/performance/ua/performancelab/MemoryLeakActivity.java b/app/src/main/java/com/performance/ua/performancelab/MemoryLeakActivity.java index 181b8db..2b83995 100644 --- a/app/src/main/java/com/performance/ua/performancelab/MemoryLeakActivity.java +++ b/app/src/main/java/com/performance/ua/performancelab/MemoryLeakActivity.java @@ -31,6 +31,17 @@ static class ListenerCollector { public void setListener(View view, MyCustomView.MyListener listener) { sListeners.put(view, listener); } + + private static void clearListeners() { + sListeners.clear(); + } + } + + @Override + protected void onStop() { + super.onStop(); + ListenerCollector.clearListeners(); } + } diff --git a/build.gradle b/build.gradle index be515a8..168f129 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.3.0' + classpath 'com.android.tools.build:gradle:2.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties deleted file mode 100644 index 877a4b7..0000000 --- a/gradle/wrapper/gradle-wrapper.properties +++ /dev/null @@ -1,6 +0,0 @@ -#Fri Apr 22 13:18:03 EEST 2016 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip