2015-08-28 80 views
2

我爲我的應用程序寫了一個小繪圖儀。ArrayList中的更快項目<T>

該函數需要很多時間,還有另一種方法,在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[]> 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}); 
      } 

這個代碼是我的問題,它的NEET長爲133項 繪製belowe有

for(int inde = 0; inde <= yWerte.size()-2; inde++) 
      { 
       Float[] xy = yWerte.get(inde); 
       Float[] xyII = yWerte.get(inde + 1); 
       canvas.drawLine((xy[0] * step) + xMitte, (xy[1] * step) + yMitte, (xyII[0] * step) + xMitte, (xyII[1] * step) + yMitte, fc); 
      } 

圓浮

private float getF(float y) 
{ 
    DecimalFormat df = new DecimalFormat("#.##"); 
    String twoDigitNum = df.format(y).replace(",","."); 
    Log.d("getF", twoDigitNum); 
    return Float.parseFloat(twoDigitNum); 
} 
+1

簡介您的應用程序,並點具體的問題在你的代碼 –

+1

問題SI不是ArrayList中,它的字符串解析和格式化。 – Natix

+0

此外,(聯合國)拳擊可以降低性能。使用'float []'而不是'Float' []。 – Natix

回答

0

不知道這個,但我認爲在for循環之外定義下面的變量「會產生一些更好的性能,因爲它們也會像133次那樣產生...

String a; 
 
float y; 
 
float[] xy; 
 
float[] xyII;

+0

好吧,我嘗試,等待 我有看這個網頁:http://www.devahead.com/blog/2011/12/coding-for-performance-and-avoiding -garbage-collection-in-android/ 但是沒有幫助 – Facebamm

+0

以及如何在一個項目我浮動[]? – Facebamm

1
  • 不做平局內部分配。將它們作爲對象初始化的一部分,或者一旦知道在接收某些數據時將繪製什麼內容。看起來像整個第一部分可以移動到您的繪圖代碼之外,並且只有在它改變時才重新計算。

  • 不要使用列表yWerte,你可以做一個完美的數組。這將避免撥打get()

  • 不要使用Float其中float會做得很好。正如其他人指出的,boxing and unboxing也表現出色。

  • 您甚至可以預先計算每個x,y座標的位置,並避免draw方法中的附加算術運算。這意味着基本上移動繪圖之外的所有東西,並預先使用線條的x,y座標預先計算一個float[][]數組。

+0

好吧,起初我翻譯,它可以需要一些時間好 – Facebamm

+0

有我的代碼,當我完成後,我再次發佈http://pastebin.com/cFg4YvCq – Facebamm

+0

O,我讀了一點關於「拳擊和拆箱」是不使用強制轉換和當前數據類型?只有 – Facebamm

0

//你正在執行的第一個代碼塊關閉引出線,對不對?

我看到的一個優化是,每個xy使用完全相同的乘法和加法,因此您可以預先計算這些乘法和加法。另外,這裏有什麼其他人建議使用原始的float是一個非常好的主意。此外,使用List.toArray()獲得原始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的Android。2)

不過,可以肯定,也使你的代碼看起來更鐵桿的副作用,這裏的意圖是在非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); 
} 

PS看看android performance guide

+0

好吧,起初我翻譯,它可以花一些時間 – Facebamm

+1

好吧我已經改變了想法,你可以看看那裏,這樣好嗎? http://pastebin.com/mXDEDpqt – Facebamm

+0

@DanielBaumertHackerOnBoard沒有隊友,不要使用迭代器(foreach語法),如果沒有JIT它會變慢。但重用的想法非常整齊。 –