Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 324
Elie Avatar asked Nov 13 '22 06:11

Elie


1 Answers

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.

like image 109
D-B Avatar answered Nov 14 '22 21:11

D-B