I tested my app in MotoG 2nd Gen (1GB RAM, Quad-core 1.2 GHz). I have the following static method in a class Misc. The itemRates array is an Object array of length : 132 containing Strings of around length of 50 characters:-
public static double getDCRate(String item){
for(int i=0;i<itemRates.length;i++){
String ar[] = (itemRates[i]+"").split("[;]");
if(ar[0].equals(item)){
return Double.parseDouble(ar[1]);
}
}
return 0;
}
Now, in my AsyncTask, I call this function as shown below:-
/* db initialized */
Cursor c = db.rawQuery(sql, null);
if (c.moveToFirst()) {
do {
/* ... */
double totalCost = 0.0;
double ntwt_double = c.getDouble(c.getColumnIndex("Ntwt"));
totalCost += Misc.getDCRate(c.getString(c.getColumnIndex("DN1")))*(c.getDouble(c.getColumnIndex("DP1"))/100)*ntwt_double;
totalCost += Misc.getDCRate(c.getString(c.getColumnIndex("DN2")))*(c.getDouble(c.getColumnIndex("DP2"))/100)*ntwt_double;
totalCost += Misc.getDCRate(c.getString(c.getColumnIndex("DN3")))*(c.getDouble(c.getColumnIndex("DP3"))/100)*ntwt_double;
totalCost += Misc.getDCRate(c.getString(c.getColumnIndex("DN4")))*(c.getDouble(c.getColumnIndex("DP4"))/100)*ntwt_double;
totalCost += Misc.getDCRate(c.getString(c.getColumnIndex("DN5")))*(c.getDouble(c.getColumnIndex("DP5"))/100)*ntwt_double;
totalCost += Misc.getDCRate(c.getString(c.getColumnIndex("DN6")))*(c.getDouble(c.getColumnIndex("DP6"))/100)*ntwt_double;
totalCost += Misc.getDCRate(c.getString(c.getColumnIndex("DN7")))*(c.getDouble(c.getColumnIndex("DP7"))/100)*ntwt_double;
totalCost += Misc.getDCRate(c.getString(c.getColumnIndex("DN8")))*(c.getDouble(c.getColumnIndex("DP8"))/100)*ntwt_double;
It takes around 12 seconds for the AsyncTask to complete..Eh?! Fairly large for itering a small array. To check that it was the function call, I removed the function calls to find the AsyncTask completing in about 1 second! Point to be noted is that I use the same lines of code in my swing application, which is also a Java framework. So why does it take so much time to execute in android? Any idea how to solve this dilemma?
Aucun commentaire:
Enregistrer un commentaire