Android ArrayList中的项目速度更快<;T>;
我正在为我的应用程序编写一个小绘图仪 这个函数需要很多时间,有没有其他方法,可以在画布上画得更快Android ArrayList中的项目速度更快<;T>;,android,performance,arraylist,android-canvas,Android,Performance,Arraylist,Android Canvas,我正在为我的应用程序编写一个小绘图仪 这个函数需要很多时间,有没有其他方法,可以在画布上画得更快 String auggabe = "x+2"; float xMitte = step * (count_w/2); //stepp is 100px, count_w is 20 float yMitte = step * (count_h/2); //count_h is 10 ArrayList<Float[]&g
String auggabe = "x+2";
float xMitte = step * (count_w/2); //stepp is 100px, count_w is 20
float yMitte = step * (count_h/2); //count_h is 10
ArrayList<Float[]> yWerte = new ArrayList<>();
for(float i = (count_w/2 * -1);i <= count_w/2; i = getF(i + 0.15f))
{
Log.d("FC", Float.toString(i));
String a = new math_compile(auggabe.replace("x", String.valueOf(i)), 0).getAufgabe();//is a class they give me the arithmetic problem value
float y = Float.parseFloat(a.replace(" ","")) ;
yWerte.add(new Float[]{i, y*-1});
}
对此不确定,但我认为在for循环之外定义以下变量“将带来更好的性能,因为它们也将生成133次
字符串a;
浮动y;
浮动[]xy;
浮动[]xyII;
- 不要在绘图内进行分配。将分配作为对象初始化的一部分进行,或者在您知道接收到某些数据时将绘制什么时进行分配。看起来整个第一部分可以移动到绘图代码之外,并且只有在绘图代码更改时才重新计算
- 不要将列表用于
,可以很好地使用数组。这将避免yWerte
调用get()
- 不要使用
,因为Float
可以做得很好。正如其他人所指出的,它也会影响性能Float
- 您甚至可以根据绘制的直线预先计算每个x,y坐标的位置,并避免
方法本身中的额外算术运算。这意味着基本上将所有内容移到绘图之外,并使用直线的x,y坐标预计算draw
数组float[][
xy
都使用完全相同的乘法和加法,因此可以预先计算它们。此外,这里的其他人建议使用primitivefloat
是一个非常好的主意。另外,使用List.toArray()
获得primitivefloat[]
数组:
// off-draw
yWerte.add(new float[]{i * step + xMitte, y * -1 * step + yMitte});
float[][] arrWerte = yWerte.toArray();
// draw
float[] xy = arrWerte[inde];
float[] xyII = arrWerte[inde + 1];
canvas.drawLine((xy[0], xy[1], xyII[0], xyII[1], fc);
现在,对于在循环之外执行变量声明来说——这是一件好事,但实际上JIT会为您做到这一点(大师,如果我错了,请纠正我),因此您可能不会在任何当代环境中获得任何明显的速度提升(从2.2开始的任何Android)
不过,可以肯定的是,作为一种副作用,您的代码看起来更为核心,下面是一个旨在在非JIT环境中更快的周期实现:
// avoid property lookups using local vars
int werteLength = arrWerte.length - 2;
float[][] localArrWerte = arrWerte;
for(int inde = 0; inde <= werteLength; inde++) {
float[] xy = arrWerte[inde];
float[] xyII = arrWerte[inde + 1];
canvas.drawLine((xy[0], xy[1], xyII[0], xyII[1], fc);
}
//避免使用本地变量查找属性
int WERTELENGHT=arrWerte.length-2;
浮动[][]本地arrWerte=arrWerte;
对于(int inde=0;inde评测应用程序并指出代码中的特定问题问题问题不是ArrayList,而是字符串解析和格式化问题。此外,(取消)装箱可能会降低性能。使用float[]
而不是float
[].x+2
是一个例子吗?是的,x+2是一个测试的例子。好吧,等等,我看了这页:但是没有帮助,如何在一个项目上使用浮动[]?这是我的代码,当我完成后,我再次发布。O,我读了一点关于“装箱和拆箱”的内容“这是为了不使用强制转换和当前的数据类型??Onlyok我有一个想法,你可以看看那里,这样可以吗?@DanielBaumertHackerOnBoard No mate,不要使用迭代器(foreach语法),如果没有JIT,速度会慢一些。但是重用的想法很好。好的,我尝试编程
// off-draw
yWerte.add(new float[]{i * step + xMitte, y * -1 * step + yMitte});
float[][] arrWerte = yWerte.toArray();
// draw
float[] xy = arrWerte[inde];
float[] xyII = arrWerte[inde + 1];
canvas.drawLine((xy[0], xy[1], xyII[0], xyII[1], fc);
// avoid property lookups using local vars
int werteLength = arrWerte.length - 2;
float[][] localArrWerte = arrWerte;
for(int inde = 0; inde <= werteLength; inde++) {
float[] xy = arrWerte[inde];
float[] xyII = arrWerte[inde + 1];
canvas.drawLine((xy[0], xy[1], xyII[0], xyII[1], fc);
}