Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ public CanvasApiView(Context context, AttributeSet attrs, int defStyleAttr) {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int cardX;
int shiftI;
for (int i = 0; i < N; i++) {
// Each card is laid out a little to the right of the previous one.
myPaint.setColor(Color.RED / (i + 1));
myPaint.setStrokeWidth(10);
canvas.drawRect(MARGIN + i * shift, MARGIN, SIZE + i * shift, SIZE, myPaint);
canvas.save();
shiftI = i * shift;
cardX = shiftI + MARGIN;
if (i != N - 1) {
canvas.clipRect(cardX, MARGIN, cardX + shift, SIZE);
}
canvas.drawRect(cardX, MARGIN, SIZE + shiftI, SIZE, myPaint);
//Or just draw needed rectangles
canvas.restore();
}
// Invalidate the whole view. Doing this calls onDraw() if the view is visible.
invalidate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import android.view.View;
import android.webkit.WebView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;

/**
Expand All @@ -22,6 +24,8 @@ public static void start(Context context) {
context.startActivity(new Intent(context, ContainerActivity.class));
}

static HashMap<Integer, ArrayList<Integer>> hashMap = new HashMap<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -53,25 +57,32 @@ public void dumpPopularRandomNumbersByRank() {
// 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 < sortedNumbers.length; i++) {
Integer currentNumber = sortedNumbers[i];
for (int j = 0; j < coolestRandomNumbers.length; j++) {
if (currentNumber.compareTo(coolestRandomNumbers[j]) == 0) {
Log.i("Popularity Dump", currentNumber + ": #" + j);
for (int i = 0; i < coolestRandomNumbers.length; i++) {
if(hashMap.containsKey(coolestRandomNumbers[i])) {
hashMap.get(coolestRandomNumbers[i]).add(i);
} else {
ArrayList<Integer> temp = new ArrayList<>();
temp.add(i);
hashMap.put(coolestRandomNumbers[i], temp);
}
}
for (Integer sortNumber : sortedNumbers) {
if(hashMap.containsKey(sortNumber)) {
ArrayList<Integer> indexes = hashMap.get(sortNumber);
for (Integer index:indexes) {
Log.i("Popularity Dump", sortNumber + ": #" + index);
}
}
}
Trace.endSection();
}

public static Integer[] coolestRandomNumbers = new Integer[3000];
static int temp;

static {
Random randomGenerator = new Random();
for (int i = 0; i < 3000; i++) {
temp = randomGenerator.nextInt();
coolestRandomNumbers[i] = temp;
coolestRandomNumbers[i] = randomGenerator.nextInt();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

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;
import android.util.SparseArray;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import java.util.Date;
import java.util.HashMap;

/**
* Created by sergey on 4/22/16.
*/
public class FibActivity extends AppCompatActivity {

public static final String TAG = FibActivity.class.getSimpleName();
public static final int POSITION_IN_FIB_SEQUENCE = 20;
public static final int POSITION_IN_FIB_SEQUENCE = 40;
private TextView textView;
private HashMap<Integer, Integer> mCacheMap = new HashMap<>();

public static void start(Context context) {
context.startActivity(new Intent(context, FibActivity.class));
Expand All @@ -30,7 +36,15 @@ 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)));
for (int i = 0; i < 4; i++) {
long time = new Date().getTime();
String value = String.valueOf(computeFibonacci(POSITION_IN_FIB_SEQUENCE));
Log.d(TAG, String.valueOf(new Date().getTime() - time) + " ====" + value);
time = new Date().getTime();
String.valueOf(computeFibonacciOptimized(POSITION_IN_FIB_SEQUENCE));
Log.d(TAG, String.valueOf(new Date().getTime() - time) + " ====" + value);
new StupidAsyncTask().doInBackground(new Integer[]{POSITION_IN_FIB_SEQUENCE});
}
}
});
WebView webView = (WebView) findViewById(R.id.anim_view);
Expand All @@ -40,7 +54,7 @@ public void onClick(View v) {
}

public int computeFibonacci(int positionInFibSequence) {
Log.i(TAG, "inside fib" + positionInFibSequence);
// Log.i(TAG, "inside fib" + positionInFibSequence);
if (positionInFibSequence <= 2) {
return 1;
} else {
Expand All @@ -49,4 +63,34 @@ public int computeFibonacci(int positionInFibSequence) {
}
}

public int computeFibonacciOptimized(int positionInFibSequence) {
if(mCacheMap.containsKey(positionInFibSequence)) {
return mCacheMap.get(positionInFibSequence);
}
// Log.i(TAG, "inside fib" + positionInFibSequence);
if (positionInFibSequence <= 2) {
mCacheMap.put(positionInFibSequence, 1);
return 1;
} else {
int number = computeFibonacciOptimized(positionInFibSequence - 1)
+ computeFibonacciOptimized(positionInFibSequence - 2);
mCacheMap.put(positionInFibSequence, number);
return number;
}
}

private class StupidAsyncTask extends AsyncTask<Integer, Integer, Integer> {

@Override
protected Integer doInBackground(Integer[] params) {
return computeFibonacci(params[0]);
}

@Override
protected void onPostExecute(Integer aVoid) {
super.onPostExecute(aVoid);
String.valueOf(aVoid);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.performance.ua.performancelab;

import android.view.View;

import java.util.WeakHashMap;

/**
* @author Evgeniy Tkachenko
* @since 4/23/16.
*/
public class ListenerCollector {
// A common case is to want to store all the listeners for a particular type of view
// somewhere. Harmless AND convenient. Or... is it? o_0
static private WeakHashMap<View, MyCustomView.MyListener> sListeners = new WeakHashMap<View, MyCustomView.MyListener>();

public static void setListener(View view, MyCustomView.MyListener listener) {
sListeners.put(view, listener);
}

public static void removeListeners(View view) {
sListeners.remove(view);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ public void imPrettySureSortingIsFree() {
}

// Now go through and dump the sorted version of each row to output!
StringBuilder builder = new StringBuilder();
for (int i = 0; i < lotsOfInts.length; i++) {
String rowAsStr = "";
builder = 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];
// rowAsStr += getSorted(lotsOfInts[i])[j];
builder.append(getSorted(lotsOfInts[i])[j]);
if (j < (lotsOfInts[i].length - 1)) {
rowAsStr += ", ";
// rowAsStr += ", ";
builder.append(", ");
}
}
Log.i("MemoryChurnActivity", "Row " + i + ": " + rowAsStr);
Log.i("MemoryChurnActivity", "Row " + i + ": " + builder.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,4 @@ protected void onCreate(Bundle savedInstanceState) {
View view = findViewById(R.id.memory_view);
}

static class ListenerCollector {
// A common case is to want to store all the listeners for a particular type of view
// somewhere. Harmless AND convenient. Or... is it? o_0
static private WeakHashMap<View, MyCustomView.MyListener> sListeners = new WeakHashMap<View, MyCustomView.MyListener>();

public void setListener(View view, MyCustomView.MyListener listener) {
sListeners.put(view, listener);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public interface MyListener {
* Internal initialization procedures for this view, regardless of which constructor was called.
*/
private void init() {
MemoryLeakActivity.ListenerCollector collector = new MemoryLeakActivity.ListenerCollector();
collector.setListener(this, mListener);
ListenerCollector.setListener(this, mListener);
}

public MyCustomView(Context context) {
Expand All @@ -42,4 +41,10 @@ public void someListenerCallback() {
System.out.println("Someone called me!");
}
};

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
ListenerCollector.removeListeners(this);
}
}
6 changes: 3 additions & 3 deletions app/src/main/res/layout/backgrounds_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/backgrounds_title"
android:background="@android:color/white"/>
/>

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/backgrounds_list"
android:background="@android:color/white"/>
/>

</LinearLayout>
52 changes: 22 additions & 30 deletions app/src/main/res/layout/hierarchy_layout.xml
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_margin="@dimen/avatar_layout_margin"
>

<!-- Version 1. Uses nested LinearLayouts -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView
android:id="@+id/chat_author_avatar1"
android:layout_width="@dimen/avatar_dimen"
android:layout_height="@dimen/avatar_dimen"
android:src="@mipmap/ic_launcher"
android:contentDescription="@string/test" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:orientation="horizontal">

<ImageView
android:id="@+id/chat_author_avatar1"
android:layout_width="@dimen/avatar_dimen"
android:layout_height="@dimen/avatar_dimen"
android:layout_margin="@dimen/avatar_layout_margin"
android:src="@mipmap/ic_launcher" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:text="@string/line1_text"
android:id="@+id/awesome_text"
android:layout_toEndOf="@+id/chat_author_avatar1"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/line1_text" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/line2_text" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/line2_text"
android:layout_toEndOf="@+id/chat_author_avatar1"
android:layout_below="@+id/awesome_text"
/>

</LinearLayout>
</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<string name="battery_historian_toast">Show how to use historian</string>
<string name="churn_toast">use traceview to locate churn</string>
<string name="scheduler_btn_label">init download</string>
<string name="test">test</string>
</resources>