Wednesday 2 January 2013

Measure Performance Time Using NDK.


Difference between Performance time in NDK in JAVA through One Application.

Create MathLib class and put below code.


package com.anky.mathsndk;

import android.util.Log;

public class MathLib {
private static final String TAG = "Math_lib";

private static long fib(long n) {
return n <= 0 ? 0 : n == 1 ? 1 : fib(n - 1) + fib(n - 2);
}

// Recursive Java implementation of the Fibonacci algorithm
// (included for comparison only)
public static long fibJR(long n) {
Log.d(TAG, "fibJR(" + n + ")");
return fib(n);
}

// Function prototype for future native recursive implementation
// of the Fibonacci algorithm
public native static long fibNR(long n);

// Iterative Java implementation of the Fibonacci algorithm
// (included for comparison only)
public static long fibJI(long n) {
Log.d(TAG, "fibJI(" + n + ")");
long previous = -1;
long result = 1;
for (long i = 0; i <= n; i++) {
long sum = result + previous;
previous = result;
result = sum;
}
return result;
}

public static long factorialJ(long n) {

long fact = 1;
Log.e(TAG, "" + "Factorial of " + n + ":");
// System.out.println("Factorial of " + n + ":");
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
return fact;

}

// Function prototype for future iterative recursive implementation
// of the Fibonacci algorithm
public native static long fibNI(long n);

public native static long factorialN(long n);

static {
// as defined by LOCAL_MODULE in Android.mk
System.loadLibrary("com_anky_mathsndk_MathLib");
}
}


Create MainActivity and Put below code.

package com.anky.mathsndk;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

private EditText input;

private RadioGroup type;

private TextView output;

private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maths_test);
this.input = (EditText) super.findViewById(R.id.input);
this.type = (RadioGroup) super.findViewById(R.id.type);
this.output = (TextView) super.findViewById(R.id.output);
Button button = (Button) super.findViewById(R.id.button);
button.setOnClickListener(this);

context = MainActivity.this;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s = this.input.getText().toString();
if (TextUtils.isEmpty(s)) {
return;
}
final ProgressDialog dialog = ProgressDialog.show(context, "",
"Calculating...", true);
final long n = Long.parseLong(s);
new AsyncTask<Void, Void, String>() {

@Override
protected String doInBackground(Void... params) {
long result = 0;
long t = SystemClock.uptimeMillis();
switch (MainActivity.this.type.getCheckedRadioButtonId()) {
case R.id.type_fib_jr:
result = MathLib.fibJR(n);
break;
case R.id.type_fib_ji:
result = MathLib.fibJI(n);
break;
case R.id.type_fib_nr:
result = MathLib.fibNR(n);
break;
case R.id.type_fib_ni:
result = MathLib.fibNI(n);
break;
case R.id.type_fact_j:
result = MathLib.factorialJ(n);
break;
case R.id.type_fact_n:
result = MathLib.factorialN(n);
break;
}
t = SystemClock.uptimeMillis() - t;
return String.format("(%d)=%d in %d ms", n, result, t);
}

@Override
protected void onPostExecute(String result) {
dialog.dismiss();
MainActivity.this.output.setText(result);
}
}.execute();

}

}




Create jni directory project and Create Android.mk file in jni and Put this code.

LOCAL_PATH := $(call my-dir)


include $(CLEAR_VARS)


LOCAL_LDLIBS := -llog

LOCAL_MODULE := com_anky_mathsndk_MathLib
LOCAL_SRC_FILES := com_anky_mathsndk_MathLib.c


include $(BUILD_SHARED_LIBRARY)


Generate header file using External Tools or Terminal com_anky_mathsndk_MathLib.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_indianic_mathsndk_MathLib */

#ifndef _Included_com_indianic_mathsndk_MathLib
#define _Included_com_indianic_mathsndk_MathLib
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_indianic_mathsndk_MathLib
 * Method:    fibNR
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_com_anky_mathsndk_MathLib_fibNR
  (JNIEnv *, jclass, jlong);

/*
 * Class:     com_indianic_mathsndk_MathLib
 * Method:    fibNI
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_com_anky_mathsndk_MathLib_fibNI
  (JNIEnv *, jclass, jlong);

/*
 * Class:     com_indianic_mathsndk_MathLib
 * Method:    factorialN
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_com_anky_mathsndk_MathLib_factorialN
  (JNIEnv *, jclass, jlong);

#ifdef __cplusplus
}
#endif
#endif


Create com_anky_mathsndk_MathLib.c file and put this code.

#include "com_anky_mathsndk_MathLib.h"
#include <android/log.h>

static jlong fib(jlong n) {
return n <= 0 ? 0 : n == 1 ? 1 : fib(n - 1) + fib(n - 2);
}

JNIEXPORT jlong JNICALL Java_com_anky_mathsndk_MathLib_fibNR(JNIEnv* env,
jclass clazz, jlong n) {
__android_log_print(ANDROID_LOG_DEBUG, "FibLib.c", "fibNR(%lld)", n);
return fib(n);

}



JNIEXPORT jlong JNICALL Java_com_anky_mathsndk_MathLib_fibNI(JNIEnv *env,
jclass clazz, jlong n) {

jlong previous = -1;
jlong result = 1;
jlong i;
__android_log_print(ANDROID_LOG_DEBUG, "FibLib.c", "fibNI(%lld)", n);
for (i = 0; i <= n; i++) {
jlong sum = result + previous;
previous = result;
result = sum;
}
return result;
}

JNIEXPORT jlong JNICALL Java_com_anky_mathsndk_MathLib_factorialN(
JNIEnv *env, jclass clazz, jlong n) {
jlong fact = 1;
jlong i;
for (i = 1; i <= n; i++) {
fact = fact * i;
}
return fact;


}



Compile the project using NDKBuild and Run the project.
 

You have to notice the difference between JAVA and NDK.

Execution time for factorial and Fibonaci series of number in NDK is faster than JAVA.





10 comments:

  1. Nice Blog Dude.. keep it up... continue... post the blog to improve the knowledge for native android.

    ReplyDelete
  2. I have been a regular visitor of this site and I love reading blogs posted here. They are truly very well written, precise and to the point. Thanks. best lasik surgery in india

    ReplyDelete
  3. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
    website development company sydney| ios app development

    ReplyDelete
  4. I have joined your feed and sit up for searching for more of your great post.
    Additionally, I have shared your web site in my social networks.

    appvn app

    ReplyDelete
  5. I was recommended this web site by means of my cousin.
    I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!

    selenium training in Chennai
    selenium training in Tambaram
    selenium training in Velachery
    selenium training in Omr
    selenium training in Annanagar


    After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
    Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in

    ReplyDelete
  6. very specific nice content. This article is very much helpful and i hope this will be an useful information for the needed one.Keep on updating these kinds of informative things
    I likable the posts and offbeat format you've got here! I’d wish many thanks for sharing your expertise and also the time it took to post!!
    AIO https://aiodownloaderatoz.blogspot.com
    TrackView Pro https://aiodownloaderatoz.blogspot.com/2018/03/trackview-pro-248-pro.html
    CamScanner -Phone PDF https://aiodownloaderatoz.blogspot.com/2018/01/camscanner-phone-pdf-creator.html
    Neutrino+ https://aiodownloaderatoz.blogspot.com/2018/04/neutrino-191-mod.html
    Pocket Mine https://aiodownloaderatoz.blogspot.com/2018/04/pocket-mine-3-450-apk-mod-unlimited.html

    ReplyDelete
  7. Good post. Thank you for sharing.
    moviebox for mac

    ReplyDelete