| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.android.startop.test; |
| 22 |
|
| 23 |
import android.app.Activity; |
| 24 |
|
| 25 |
public class CPUIntensiveBenchmarks { |
| 26 |
public static final int ARRAY_SIZE = 30000; |
| 27 |
public static int[][] mArray; |
| 28 |
|
| 29 |
static class WorkerThread extends Thread { |
| 30 |
int mThreadNumber; |
| 31 |
WorkerThread(int number) { |
| 32 |
mThreadNumber = number; |
| 33 |
} |
| 34 |
public void run() { |
| 35 |
final int arrayLength = mArray[mThreadNumber].length; |
| 36 |
for (int i = 0; i < arrayLength; ++i) { |
| 37 |
mArray[mThreadNumber][i] = i * i; |
| 38 |
} |
| 39 |
for (int i = 0; i < arrayLength; ++i) { |
| 40 |
for (int j = 0; j < arrayLength; ++j) { |
| 41 |
int swap = mArray[mThreadNumber][j]; |
| 42 |
mArray[mThreadNumber][j] = mArray[mThreadNumber][(j + i) % arrayLength]; |
| 43 |
mArray[mThreadNumber][(j + i) % arrayLength] = swap; |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
static void doSomeWork(int threadCount) { |
| 50 |
mArray = new int[threadCount][ARRAY_SIZE]; |
| 51 |
WorkerThread[] threads = new WorkerThread[threadCount]; |
| 52 |
|
| 53 |
for (int i = 0; i < threadCount; ++i) { |
| 54 |
threads[i] = new WorkerThread(i); |
| 55 |
} |
| 56 |
|
| 57 |
for (int i = 0; i < threadCount; ++i) { |
| 58 |
threads[i].start(); |
| 59 |
} |
| 60 |
|
| 61 |
for (int i = 0; i < threadCount; ++i) { |
| 62 |
try { |
| 63 |
threads[i].join(); |
| 64 |
} catch (Exception ex) { |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
public static final int TIME_LIMIT = 5; |
| 71 |
|
| 72 |
static void initializeBenchmarks(Activity parent, BenchmarkRunner benchmarks) { |
| 73 |
benchmarks.addBenchmark("Use 1 thread", () -> { |
| 74 |
doSomeWork(1); |
| 75 |
}); |
| 76 |
benchmarks.addBenchmark("Use 2 threads", () -> { |
| 77 |
doSomeWork(2); |
| 78 |
}); |
| 79 |
benchmarks.addBenchmark("Use 4 threads", () -> { |
| 80 |
doSomeWork(4); |
| 81 |
}); |
| 82 |
benchmarks.addBenchmark("Use 8 threads", () -> { |
| 83 |
doSomeWork(8); |
| 84 |
}); |
| 85 |
benchmarks.addBenchmark("Use 16 threads", () -> { |
| 86 |
doSomeWork(16); |
| 87 |
}); |
| 88 |
} |
| 89 |
} |