Phonegap vs native on Android performance tests

I have a serious problem with my final project. I was asked to develop a mobile application using PhoneGap to support multiple platforms. Then I was told to compare the features of PhoneGap vs Native , so I decided to test it on Android.

I wrote an example function and measured the time it took to achieve in JavaScript for PhoneGap and in Java for Native Android . And the funny thing is that the JavaScript function takes 10 times less time to complete, and all this time I thought that the Native functions are much faster.

Here is my JavaScript code for PhoneGap:

        var array = new Array();
        var start = new Date().getTime();
            for (var i = 0; i < 1000000; i++) {
                var j = i + Math.random();
                if (j % 2 == 0)
                    j = 1;
                else
                    j = 0;
                array.push(j);
            }
            var end = new Date().getTime();

            var time = end-start;
            var div = document.getElementById('result');

            div.innerHTML = "Result time= " + time;

And my Java code for Native:

            long startTime = System.currentTimeMillis();
            ArrayList<Integer> array = new ArrayList<Integer>();
            for (int i = 0; i < 1000000; i++) {
                Random r = new Random();
                int j = i + r.nextInt();
                if (j % 2 == 0)
                    j = 1;
                else
                    j = 0;
                array.add(j);
            }
            long endTime = System.currentTimeMillis();

            long time = endTime - startTime;
            t1.setTextColor(Color.BLACK);
            t1.setText("Result time= "
                    + Long.toString(time));

Output for the first: 350 ms on average

Output for the second: 3600 ms on average

I am testing

Samsung Galaxy Note 10.1 Tablet

Is it correct? Or did I miss something and made a serious mistake?

Thank you very much for your help.

-------------- -------------- Update

after posting

Random r = new Random ()

outside the cycle, the new time required to complete the cycle is 750 ms . But with the help of Phonegap, the speed is twice as fast, can I conclude that for numerical treatment Telephony is better than Native on Android?

+5
source share
2 answers

. .

, GUI, , .. GUI, . .

, , .

+1

package com.zeroblitz.benchmark1;
import java.util.ArrayList;
import java.util.Random;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView tvTest1;
private TextView tvTest2;
private Button btnStart;
private int v[]=new int[1000000];
private Random random=new Random(System.currentTimeMillis());
private int n_v=0;
private ArrayList<Integer> array=new ArrayList<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initViews();
}
private void initViews(){
    tvTest1=(TextView)findViewById(R.id.tvTest1);
    tvTest2=(TextView)findViewById(R.id.tvTest2);
    btnStart=(Button)findViewById(R.id.btnStart);

    btnStart.setOnClickListener(startButtonListener);
}
OnClickListener startButtonListener=new OnClickListener() {
    public void onClick(View v) {
        new AsyncTest1(tvTest1).execute();
    }
};

public class AsyncTest1 extends AsyncTask<Void, Void, Void>{
    private TextView tv;
    long time=0;
    public AsyncTest1(TextView tv){
        this.tv=tv;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        tv.setText("Testing 1 ....");
    }
    protected Void doInBackground(Void... params) {
        n_v=0;
        long start=System.currentTimeMillis();
        for(int i=0;i<1000000;i++)
            v[n_v++]=1-(i+random.nextInt())%2;
        time=System.currentTimeMillis()-start;
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        tv.setText(String.format("Test 1 = %.3f", time/1000.f));
        new AsyncTest2(tvTest2).execute();
    }
}
public class AsyncTest2 extends AsyncTask<Void, Void, Void>{
    private TextView tv;
    long time=0;
    public AsyncTest2(TextView tv){
        this.tv=tv;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        tv.setText("Testing 2 ....");
    }
    protected Void doInBackground(Void... params) {
        //n_v=0;
        array.clear();
        array.trimToSize();
        long start=System.currentTimeMillis();
        for(int i=0;i<1000000;i++){
            //int j=i+random.nextInt();
            int j=i+new Random().nextInt();
            if(j%2==0)j=1;
            else j=0;
            array.add(j);
        }
            //array.add(1-(i+random.nextInt())%2);
        time=System.currentTimeMillis()-start;
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        tv.setText(String.format("Test 2 = %.3f", time/1000.f));
    }
}
}
0

All Articles