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.





18 comments:

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

    ReplyDelete
    Replies
    1. Great Article Cyber Security Projects projects for cse Networking Security Projects JavaScript Training in Chennai JavaScript Training in Chennai The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  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
  7. 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
  8. 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
  9. 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
  10. 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
  11. Hello sir,
    Thank you very much for sharing this huge explosion with us. It's really helpful and informative.
    Please continue to blog new updates.

    DedicatedHosting4u.com

    ReplyDelete
  12. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks!

    SS Total Anova Calculator
    derivative calculator
    Interval Calculator
    edublackboards

    ReplyDelete