Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ArrayList中的项目速度更快<;T>;_Android_Performance_Arraylist_Android Canvas - Fatal编程技术网

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坐标的位置,并避免
    draw
    方法本身中的额外算术运算。这意味着基本上将所有内容移到绘图之外,并使用直线的x,y坐标预计算
    float[][
    数组


//您正在从绘图线程执行第一个代码块,对吗

我看到的一个优化是,每个
xy
都使用完全相同的乘法和加法,因此可以预先计算它们。此外,这里的其他人建议使用primitive
float
是一个非常好的主意。另外,使用
List.toArray()
获得primitive
float[]
数组:

// 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);
 }