- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是线程的新手(不要因为我下面的实现而杀了我:),我需要在一个单独的线程上对像素进行多次模糊处理(见下文)。它不是方框模糊的最有效实现(来自 Gaussian Filter without using ConvolveOp),但性能峰值不会出现在 Nexus 7 平板电脑上,但会出现在 Nexus 4 手机上。
我已经发布了我的测试示例(在 Android 4.2 上运行 - 见下文)。
我不认为这是由 GC 抖动内存引起的(它与峰值不一致)。
我认为这可能与缓存局部性或硬件内存抖动有关 - 但我不确定。
什么会导致尖峰?有时它们会突然发作 - 例如峰值 50%。有时它们起病缓慢 - 例如尖峰单调增加/减少,尖峰如下 -> 5%, 10%, 20%, 10%, 5%.
在进行繁重的数组处理时如何阻止它们发生?
这不会发生在我也测试过的 Nexus 7 平板电脑上(见下面的结果)
附带问题:正确休眠和重新启动我的线程的最佳方法是什么(线程新手)?
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);
}
}
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
最佳答案
我想我可能已经在某种程度上减轻了 Nexus 4 上的这种影响。计算一致性仍然存在一些可变性,但它是可以忍受的 - 我认为 - 看不到太多巨大的尖峰 - 在线程启动/关闭之外。我已经使用 Android NDK 和 c p_threads 完成了此操作,以生成一个 native 线程,该线程主要由 Java(或者我被告知)单独保留,直到更改或关闭前台应用程序。
代码如下:
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
static {
System.loadLibrary("native");
}
private native void init();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initializes and spawns native thread
init();
setContentView(R.layout.activity_main);
}
}
(应该放在Android项目根目录下的jni文件夹中)
#include <time.h>
#include <pthread.h>
#include <jni.h>
#include <android/log.h>
#define APPNAME "DELTA"
int* pixels;
int kernel_rows = 2;
int kernel_cols = 2;
int width = 60;
int height = 39;
int running = 1;
// from android samples
/* return current time in milliseconds */
static double now_ms(void) {
struct timespec res;
clock_gettime(CLOCK_REALTIME, &res);
return 1000.0 * res.tv_sec + (double) res.tv_nsec / 1e6;
}
// initialize thread/begin it
jint Java_com_example_testa_MainActivity_init(JNIEnv* env, jobject javaThis) {
int i1 = 1;
pthread_t thread;
void *run();
pthread_create(&thread, NULL, run, &i1);
pthread_join(thread, NULL);
return 0;
}
// thread function
void *run(int *x) {
// init pixels within thread
pixels = (int*) malloc(sizeof(int) * width * height);
// loop until stopped - java won't interfere
// unless closed/switch application (or so I'm told)
while (running) {
double start = now_ms();
int row, col, row_offset, col_offset;
for (row = kernel_rows / 2; row < height - kernel_rows / 2; row++) {
for (col = kernel_cols / 2; col < width - kernel_cols / 2; col++) {
float pixel = 0;
// iterate over each pixel in the kernel
for (row_offset = 0; row_offset < kernel_rows; row_offset++) {
for (col_offset = 0; col_offset < kernel_cols;
col_offset++) {
// subtract by half the kernel size to center the
// kernel
// on the pixel in question
int row_index = row + row_offset - kernel_rows / 2;
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;
}
}
double end = now_ms();
double delta = end - start;
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "%f", delta);
}
pthread_exit(0);
}
(应该放在Android项目根目录下的jni文件夹中)
LOCAL_PATH := $(call my-dir)
MY_PATH := $(LOCAL_PATH)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
LOCAL_PATH := $(MY_PATH)
LOCAL_MODULE := native
LOCAL_LDLIBS := -llog
LOCAL_SRC_FILES := native.c
include $(BUILD_SHARED_LIBRARY)
代码成本降低了约 20-30%,可变性降低了一个数量级。
代码是通过在根文件夹(可在此处找到:http://developer.android.com/tools/sdk/ndk/index.html)中从 Android 提供的 NDK 库执行 ndk-build
命令编译的。
Nexus 4(毫秒):
01-14 13:41:21.132: V/DELTA(23679): 56.554199
01-14 13:41:21.192: V/DELTA(23679): 58.568604
01-14 13:41:21.252: V/DELTA(23679): 59.484131
01-14 13:41:21.302: V/DELTA(23679): 56.768066
01-14 13:41:21.362: V/DELTA(23679): 54.692383
01-14 13:41:21.412: V/DELTA(23679): 51.823730
01-14 13:41:21.472: V/DELTA(23679): 55.668945
01-14 13:41:21.522: V/DELTA(23679): 56.920654
01-14 13:41:21.582: V/DELTA(23679): 56.371094
01-14 13:41:21.642: V/DELTA(23679): 58.507568
01-14 13:41:21.702: V/DELTA(23679): 59.697754
01-14 13:41:21.752: V/DELTA(23679): 53.990723
01-14 13:41:21.812: V/DELTA(23679): 55.669189
Nexus 7(毫秒):
01-14 13:41:25.685: V/DELTA(2916): 65.867920
01-14 13:41:25.745: V/DELTA(2916): 65.986816
01-14 13:41:25.815: V/DELTA(2916): 66.685059
01-14 13:41:25.885: V/DELTA(2916): 67.033936
01-14 13:41:25.945: V/DELTA(2916): 65.703857
01-14 13:41:26.015: V/DELTA(2916): 66.653076
01-14 13:41:26.085: V/DELTA(2916): 66.922119
01-14 13:41:26.145: V/DELTA(2916): 67.030029
01-14 13:41:26.215: V/DELTA(2916): 67.014893
01-14 13:41:26.285: V/DELTA(2916): 67.034912
01-14 13:41:26.345: V/DELTA(2916): 67.089844
01-14 13:41:26.415: V/DELTA(2916): 65.860107
01-14 13:41:26.485: V/DELTA(2916): 65.642090
01-14 13:41:26.545: V/DELTA(2916): 65.574951
01-14 13:41:26.615: V/DELTA(2916): 65.991943
关于android - 在 Nexus 4 上进行计算量大的数组处理时,会出现周期性的性能峰值是什么原因造成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14299288/
我尝试获取processID的峰值(win7音频混音器中绿色条的电平),例如5640-Spotify。 在互联网上搜索了一个好的图书馆。我找到了CSCore。 现在我有这个: class Progra
这里描述并回答了问题:https://groups.google.com/forum/#!topic/redis-db/egyA1xvhGfo 很遗憾,我没有完全理解答案。 我担心的是,如果 Redi
我有一些音频数据(浮点数数组),我用它来绘制一个简单的 波形。 绘制时,波形不会在边缘达到最大值。 没问题 - 数据只需要标准化。我迭代一次以找到最大值,然后再次迭代将每个除以最大值。再次绘制,一切看
将数组划分为最大数目的相同大小的块,每个块应包含一个索引P,以使A [P-1] A [P + 1]。 我的解决方案:golang solution 但是部分性能测试无故失败,有人可以提出一些建议吗?
我们有一个专用的数据库服务器,在 linux debian 上运行 PostgreSQL 8.3。定期查询数据库以获取大量数据,同时更新/插入也经常发生。数据库会周期性地在短时间内(例如 10 秒)不
我正在使用此 python 代码的稍微修改版本来进行频率分析: FFT wrong value? 假设我在时域中有一组正弦波,它们的频率非常接近,同时共享相同的振幅。这是它们在频域中的样子,对 102
我正在尝试找到一种方法使现有(专有)Linux 进程消耗大量 CPU 或使其过于繁忙以致于停止响应其他进程但不重新生成。 目前我所拥有的只是它在用户空间中的 pid。也许对进程的某种超快速 ping
我们在应用程序中使用 Redis 处理一些数据,这非常棒。不过,我注意到 redis-server 进程偶尔会出现 cpu 和内存峰值。 这是 Giraffe dashboard来 self 们的生产
我有一个 SwiftUI 项目和一个绑定(bind)到 EnvironmentObject 的 View 。该对象包含一个@Published 属性。 import Foundation class
我们目前使用的 VMOptions 如下所示,用于我们使用 spring webflux 设计的微服务之一。我们目前为每个 pod 预留了 4 个 CPU 和 5 GB 内存。 -Xms4096m -
我们目前使用的 VMOptions 如下所示,用于我们使用 spring webflux 设计的微服务之一。我们目前为每个 pod 预留了 4 个 CPU 和 5 GB 内存。 -Xms4096m -
我正在拼命寻找由于 CPU 峰值(18-19 个核心,100% 负载)导致的 Eclipse 卡住瞬间(最多 2-3 秒)的问题。在峰值期间什么都不起作用,整个 eclipse 只是没有反应。自从我修
我的工作涉及振动,我试图从 FFT 幅度中获取以下信息: 峰值到峰值 峰值 RMS 我正在对一个简单的正弦波函数执行 FFT,考虑汉宁窗。请注意,正弦波函数的“全振幅”为 5,运行 FFT 下面的代码
我们有一个位于负载平衡器后面的服务器池。这个池中的机器平均每 6 秒进行一次垃圾收集。垃圾收集需要将近半秒的时间。我们还看到在垃圾收集期间出现 CPU 峰值。 客户端计算机在一天内连接到服务器的平均时
有人知道为什么我们的 Amazon RDS 数据库的 CPU 使用率每小时会出现峰值吗?我们没有每小时运行任何 cron,因此它似乎是一些内部 RDS 的东西,因为它恰好是每小时运行一次。 RDS 是
我试图发现为什么 mysqld 有时会使 cpu 饱和并停止。 我怀疑这与更新索引或其他此类维护有关。我想证明这个假设并寻找避免它的选项。 情况是这样的。我有几十张 table ,但根据事件,似乎至少
已解决 在我们的例子中,问题是对于 SuggestRequestHandler (requestHandler name="/suggest") 现在已经设置了 facelimit:10此外,应用程序
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我正在尝试使用 ALSA 和 C 检索当前音频峰值。我希望将该值作为 0.0 到 1.0(或者可能是 0 到 100)之间的标量。在 Windows 上我使用 IAudioMeterInformati
我是一名优秀的程序员,十分优秀!