Elie
Elie

Reputation: 337

Phonegap vs native on android performance test

I have a serious issue with my final year project. I was told to develop a mobile app using PhoneGap to support several platforms. Then I was told to compare the performances of PhoneGap vs Native so I decided to test it on android.

I wrote a sample function and measured the time to achieve it in JavaScript for PhoneGap and in Java for Native Android. And the funny thing is that the JavaScript function is taking 10 times less time to finish and all along I thought that Native functions are a lot faster.

Here's my code in JavaScript 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 code in Java 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 first one is: 350ms on average

Output for second one is: 3600ms on average

I am testing on

Samsung Galaxy Note 10.1 tablet.

Is this right? Or am I missing something and committing a grave mistake?

Thanks a lot for you help.

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

after putting the

Random r = new Random()

outside the loop the new time it requires to execute the loop is 750ms. But with Phonegap the speed is still twice as fast, can i conclude that for numerical treatment, Phonegap is better than Native on Android?

Upvotes: 5

Views: 3083

Answers (2)

ZeroBlitz
ZeroBlitz

Reputation: 1

Here , try this

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

Upvotes: 0

D-B
D-B

Reputation: 66

Not all random number generators perform equally. For a fair comparison you would be better off doing a deterministic calculation.

Your testing should also include other functionality such as testing GUI response times, networking, database access etc. The most interesting will be GUI response times because this is where most applications will spend most of their time. All other comparisons would be like looking for micro optimisations in code.

Please share your results because that is really why I visited this question.

Upvotes: 1

Related Questions