0

I'm trying to develop an android application which reads input from text files and runs an algorithm on it. My code is as below:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alg);

    ////////////////////////////// main //////////////////////////////
    int i, j;          //loop counters
    int N;
    float productin2 = 2;
    double[] dly2 = new double[nh2];
    double sum2 = 0;
    double[] coef =  { 0, 0.01309854072, 0.06750108302, 0.1665802151, 0.252820164, 0.252820164, 0.1665802151, 0.06750108302, 0.01309854072, 0 };
    double[] hEnvs =  { 0.01579775570525054600, 0.01781403140084924000, 0.02007602712902524500, 0.02261170464938222500, 0.02545186237753081800, 0.02863036080004565900, 0.03218435360188533200, 0.03615452170070227600, 0.04058530654558931800, 0.04552513804246406700, 0.05102665129194776400, 0.05714688494379131100, 0.06394745235839295600, 0.07149467489305960700, 0.07985966446974707100, 0.08911834010325860000, 0.09935136024637487900, 0.11064394961563757000, 0.12308559557752539000, 0.13676958518580262000, 0.15179234956408211000, 0.16825257753603512000, 0.18625005525402527000, 0.20588418313000478000, 0.22725211573518173000, 0.25044646466527443000, 0.27555249889375555000, 0.30264477217428859000, 0.33178310303988118000, 0.36300783046335494000, 0.39633426806429845000, 0.43174628288460160000, 0.46918893252356092000, 0.50856010852041422000, 0.54970115646932416000, 0.59238647721601956000, 0.63631216212327169000, 0.68108378322949126000, 0.72620355172417450000, 0.77105718249523736000, 0.81490096727059447000, 0.85684977491951841000, 0.89586697826322947000, 0.93075766895485579000, 0.96016698625948826000, 0.98258597735805331000, 0.99636815747833529000, 0.99976088628954152000, 0.99095686993068455000, 0.96817259381709131000, 0.92976236102904553000, 0.87437894148283268000, 0.80119473425728838000, 0.71020093871482237000, 0.60260667694135717000, 0.48136550222324170000, 0.35186349880678341000, 0.22281151005596159000, 0.10739426873747662000, 0.02474176084283331200 };
    double sum = 0;

    double[] a_bp =  { 1.00000000000000000000, -10.74091648341686200000, 55.71763903406161900000, -184.23130974989306000000, 432.65788400377841000000, -762.10904937747364000000, 1037.22401660394640000000, -1107.35260286319680000000, 931.72411890039439000000, -614.95846638660942000000, 313.60709790142261000000, -119.95451911433545000000, 32.58814368922067200000, -5.64325562968512350000, 0.47198784101260066000, };
    double[] b_bp =  { 0.00113943092849382010, -0.01068561345428776700, 0.04726053423833388700, -0.12855488242290763000, 0.23410994143071942000, -0.28585601928705551000, 0.20343974889435235000, 0.00000000000000000000, -0.20343974889435235000, 0.28585601928705551000, -0.23410994143071942000, 0.12855488242290763000, -0.04726053423833388700, 0.01068561345428776700, -0.00113943092849382010, };

    double[] bina =  { 0.00000000000000000000, 0.58778524398803711000, 0.95105654001235962000, 0.95105654001235962000, 0.58778524398803711000, 0.000000000000000, -0.58778524398803711000, -0.95105654001235962000, -0.95105654001235962000, -0.58778524398803711000 };
    double[] nina =  { 1.00000000000000000000, 0.80901700258255005000, 0.30901700258255005000, -0.30901700258255005000, -0.80901700258255005000, -1.00000000000000000000, -0.80901700258255005000, -0.30901700258255005000, 0.30901700258255005000, 0.80901700258255005000 };
    filereader();
    j = 9;
    while (j < eFRI)
    {
        for (i = 0; i < 10; i++)
        {
            Blosi[j + i] = bina[i];
            Blosq[j + i] = nina[i];
        }
        j = j + 10;
    }

    outputiir[0] = b_bp[0] * savg1[0];
    N = M - 1;

    for (i = 1; i < N; i++)
    {
        sum = b_bp[0] * savg1[i];
        for (j = i; j >= 1; j--)
        {
            if (i - j < N && j < M)
                sum += b_bp[j] * savg1[i - j] - a_bp[j] * outputiir[i - j];
        }/*end of for*/
        outputiir[i] = sum;
        sum = 0;
    }/*end of for*/
     /* compute the outputs till nr-1*/

    for (i = 0; i < NR; i++)                //NR = 1000000-15
    {
        sum = b_bp[0] * savg1[M - 1 + i];
        for (j = 1; j <= M - 1; j++)        //M - 1 = 14
            sum += b_bp[j] * savg1[M - 1 + i - j] - a_bp[j] * outputiir[M - 1 + i - j];
        outputiir[M - 1 + i] = sum;
        sum = 0;
    }/*end for*/

    for (i = 0; i < nums; i++)
        savg1[i] = ((float)(outputiir[i]));

    for (i = 1; i < Math.floor((double)(nums / eFRI)); i++)
    {

        for (j = 0; j < eFRI; j++)
        {
            indx[j] = ((i * eFRI) - 1) + j;
            savg1[j] = savg1[j] + savg1[indx[j]];
        }
    }

    for (i = eFRI; i < nums; i++)/*atention think if you can change this num to eFRI*/
        savg1[i] = 0;

    /***********************product 1 & 2 **************************/
    for (i = 0; i < num; i++)
    {
        Blosq[i + nh1 - 1] = savg1[i] * Blosq[i + nh1 - 1];
        Blosi[i + nh1 - 1] = savg1[i] * Blosi[i + nh1 - 1];
    }/*end of for*/

    /********************FIR Filtering(lowpass) 1 & 2 **************/
    DSPF_sp_fir_gen(Blosi, coef, Blosi, nh1, num);/*lowpass FIR filter*/

    DSPF_sp_fir_gen(Blosq, coef, Blosq, nh1, num);

    for (j = 0; j < num; j++)
    {
        Blosi[j] = productin2 * Math.sqrt(Blosi[j] * Blosi[j] + Blosq[j] * Blosq[j]);
        outputiir[j] = Blosi[j]; /*env = outputiir*/
    }/*end of for*/

    /********************* match filtring ****************************/
    for (j = 0; j < (num / 5); j++)
    {
        dly2[0] = outputiir[5 * j];
        sum2 = dly2[0] * hEnvs[0];

        for (i = nh2 - 1; i > 0; i--)
        {
            sum2 += dly2[i] * hEnvs[i];
            dly2[i] = dly2[i - 1];
        }/*end of for*/

        outputiir[j] = sum2;

    }/*end of for*/

    /**************************************************************/
    int r = 0;
    sigenveval();
    r = sigfind(envpeakindx);/*output is sig_t: signals occurence time*/
    r = sigiden(sig_t, signaltlen, r);/*output is sigs: signals and their types*/
    r = sigfsync(r);/*out put is fsigs: the fine synchronization result*/
    r = sigtd(r);/*output is td : time difference between master and slaves*/
    latlong(r);
    r=1;
    TextView tv1=(TextView) findViewById(com.example.hanieh.mapapp.R.id.LngText);
    tv1.setText(Double.toString(pos[0]));

    TextView tv2=(TextView) findViewById(com.example.hanieh.mapapp.R.id.LatText);
    tv2.setText(Double.toString(pos[1]));

}

and logcat is as below:

09-23 14:25:43.485 1921-1927/com.example.hanieh.mapapp W/art: Suspending all threads took: 19.306ms
09-23 14:25:55.031 1921-1927/com.example.hanieh.mapapp W/art: Suspending all threads took: 24.494ms
09-23 14:25:55.517 1921-1927/com.example.hanieh.mapapp W/art: Suspending all threads took: 9.116ms
09-23 14:26:03.621 1921-1921/com.example.hanieh.mapapp I/Choreographer: Skipped 1263 frames!  The application may be doing too much work on its main thread.

When I debug it I'm getting warnings in the logcat window I/Choreographer: Skipped 1267 frames! The application may be doing too much work on its main thread. But visually I can’t see any problems when I run the app on my device. Answer for my question is here The application may be doing too much work on its main thread and I read it, but I can not understand it. Can anybody guide me how I can solve this warning and speed up application?

Community
  • 1
  • 1
A.Morshedian
  • 31
  • 1
  • 9

2 Answers2

1

There are 2 kind of threads you're using in your app. The first one is UI thread, which is responsible about, well, User Interface. It should basically only react to the user and updates to the UI.

The 2nd kind is Worker Thread. It does the heavy work,long calculations, network download and so on. If you're computing something inside onCreate, without starting a new thread, it takes to long to be invisible to the user, so you're app becomes unresponsive.

You have different choices here:

  1. Java Threads,
  2. AsyncTasks,
  3. Services,
  4. Reactive (RxJava)

For your specific case, first 2 are probably the best.

shadox
  • 3,238
  • 4
  • 24
  • 38
  • as you see, each step of algorithm depend on previous step, so it could not be use thread – A.Morshedian Sep 23 '16 at 12:19
  • That's not accurate. You're already using a thread, which is UI thread. You have to move all your code to a new thread. When onCreate is called, you spawn a new thread and when it's finished, you just get your data back. You're updating your UI only at the end of the algorithm. Even if it was a different approach, I don't see have this should be a problem. 2 threads, one for heavy work, second for UI changes. That's it. – shadox Sep 23 '16 at 12:54
1

You should make use of AsyncTask for these type of operation. Please go through the below link :

http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html

Do all your file and database operations in doInBackground method of AsyncTask. You won't get the exception.

The main reason for the exception is that all the processes of an android app are done in a single thread which is the main UI thread. The operations that you are performing take significant amount of time to be completed. This blocks the UI thread. Because of this you are getting the exception or error.

By using async task, your heavy operations will be done in a separate thread. As you will go through the documentation of the AsyncTask you will come to know that the doInBackground method of the AsyncTask runs in a separate thread called the worker thread or the background thread. This keeps the main UI thread alive and responsive. So please do all your file,database, network or any other time taking operation in doInBackground method of AsyncTask. You won't face the error

nishith kumar
  • 981
  • 7
  • 20