我是线程新手(不要因为我在下面的实现而杀了我:),我需要在单独的线程上进行多次像素模糊传递(见下文)。这不是盒子模糊的最有效实现(它来自Gaussian Filter 而不使用 ConvolveOp),但性能峰值不会出现在 Nexus 7 平板电脑上,但它们确实会出现在 Nexus 4 手机上。
我已经发布了我的测试样本(在 Android 4.2 上运行 - 见下文)。
我不认为这是由 GC 破坏内存引起的(它与尖峰不符)。
我认为这可能与缓存位置或硬件内存抖动有关 - 但我不确定。
什么会导致尖峰?有时它们是突然发作的——例如 50% 的峰值。有时它们起效缓慢 - 例如峰值单调增加/减少,峰值如下 -> 5%、10%、20%、10%、5%。
在进行繁重的数组处理时,如何阻止它们发生?
这不会发生在我也测试过的 Nexus 7 平板电脑上(见下面的结果)
附带问题:正确睡眠和重新启动线程的最佳方法是什么(线程新手)?
MainActivity.java
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
private MainThread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thread = new MainThread();
thread.setRunning(true);
thread.start();
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
thread.setRunning(true);
}
@Override
protected void onPause() {
super.onPause();
thread.setRunning(false);
}
}
主线程.java
package com.example.test;
import android.util.Log;
public class MainThread extends Thread {
int[] pixels;
int kernel_rows = 2;
int kernel_cols = 2;
int width = 512;
int height = 512;
@Override
public void run() {
while (running) {
long start = System.currentTimeMillis();
for (int row = kernel_rows / 2; row < height - kernel_rows / 2; row++) {
for (int col = kernel_cols / 2; col < width - kernel_cols / 2; col++) {
float pixel = 0;
// iterate over each pixel in the kernel
for (int row_offset = 0; row_offset < kernel_rows; row_offset++) {
for (int col_offset = 0; col_offset < kernel_cols; col_offset++) {
// subtract by half the kernel size to center the
// kernel
// on the pixel in question
final int row_index = row + row_offset
- kernel_rows / 2;
final int col_index = col + col_offset
- kernel_cols / 2;
pixel += pixels[row_index * width + col_index] * 1.0f / 4.0f;
}
}
pixels[row * width + col] = (int) pixel;
}
}
long stop = System.currentTimeMillis();
long delta = stop - start;
Log.d("DELTA", Long.toString(delta));
}
}
private boolean running;
public void setRunning(boolean running) {
this.pixels = new int[512 * 512];
this.running = running;
}
}
日志
Nexus 4 手机(毫秒):
01-13 10:56:05.663: D/DELTA(13507): 76
01-13 10:56:05.773: D/DELTA(13507): 107
01-13 10:56:05.843: D/DELTA(13507): 77
01-13 10:56:05.923: D/DELTA(13507): 75
01-13 10:56:06.053: D/DELTA(13507): 127
01-13 10:56:06.133: D/DELTA(13507): 78
01-13 10:56:06.213: D/DELTA(13507): 81
01-13 10:56:06.293: D/DELTA(13507): 80
01-13 10:56:06.353: D/DELTA(13507): 77
01-13 10:56:06.433: D/DELTA(13507): 79
01-13 10:56:06.513: D/DELTA(13507): 79
01-13 10:56:06.624: D/DELTA(13507): 106
01-13 10:56:06.694: D/DELTA(13507): 76
Nexus 7 平板电脑(毫秒):
01-13 11:01:03.283: D/DELTA(3909): 84
01-13 11:01:03.373: D/DELTA(3909): 85
01-13 11:01:03.453: D/DELTA(3909): 85
01-13 11:01:03.543: D/DELTA(3909): 84
01-13 11:01:03.623: D/DELTA(3909): 85
01-13 11:01:03.703: D/DELTA(3909): 84
01-13 11:01:03.793: D/DELTA(3909): 85
01-13 11:01:03.873: D/DELTA(3909): 84
01-13 11:01:03.963: D/DELTA(3909): 85
01-13 11:01:04.043: D/DELTA(3909): 84