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.





8 comments:

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

    ReplyDelete
  2. Nice post.Give it up. Thanks for share this article. For more visit:android development

    ReplyDelete
  3. Embedded system training: Wiztech Automation Provides Excellent training in embedded system training in Chennai - IEEE Projects - Mechanical projects in Chennai Wiztech provide 100% practical training, Individual focus, Free Accommodation, Placement for top companies. The study also includes standard microcontrollers such as Intel 8051, PIC, AVR, ARM, ARMCotex, Arduino etc.

    Embedded system training in chennai
    Embedded Course training in chennai
    Matlab training in chennai
    Android training in chennai
    LabVIEW training in chennai
    Arduino training in chennai
    Robotics training in chennai
    Oracle training in chennai
    Final year projects in chennai
    Mechanical projects in chennai
    ece projects in chennai

    ReplyDelete
  4. WIZTECH Automation, Anna Nagar, Chennai, has earned reputation offering the best automation training in Chennai in the field of industrial automation. Flexible timings, hands-on-experience, 100% practical. The candidates are given enhanced job oriented practical training in all major brands of PLCs (AB, Keyence, ABB, GE-FANUC, OMRON, DELTA, SIEMENS, MITSUBISHI, SCHNEIDER, and MESSUNG)

    PLC training in chennai
    Automation training in chennai
    Best plc training in chennai
    PLC SCADA training in chennai
    Process automation training in chennai
    Final year eee projects in chennai
    VLSI training in chennai

    ReplyDelete
  5. Wiztech Automation is the Leading Best IEEE Final year project Centre in Chennai and the final year students are provided complete guidance and support in their final year projects. The IEEE projects in Chennai that Wiztech Automation offers guidance and support for include complete range of system domains – such as PLC projects, embedded projects, VLSI projects, software projects, IT projects, Civil projects. Students looking for specific projects pertaining to departments of ECE, EEE, E&I, Mechanical, Mechatronics, bio-medical, IT, Computer, Civil projects in B.E, M.E, B.Tech, M.Tech, B.SC., and M.Sc Electronics, could also get turnkey solutions at Wiztech Automation Solutions to turn out successful project outcomes and models. Since the students at Wiztech Automation gain thorough theoretical and practical knowledge and skills as they pursue their final year projects and develop 2015 and 2016 Latest IEEE Projects portraying them well.

    Final year projects in chennai
    Mechanical projects in chennai
    ece projects in chennai
    Final year eee projects in chennai
    VLSI project center in chennai
    Industrial projects in chennai
    Fianl year CSE projects in chennai



    ReplyDelete
  6. 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