JNI Performance Test-Performance Comparison Between JNI Calling C and Java calling java

Source: Internet
Author: User

Performancetest. Java

Package com. jni; </p> <p> import android. app. activity; <br/> import android. OS. bundle; <br/> import android. util. log; </p> <p> public class PerformanceTest extends Activity {<br/> static <br/>{< br/> try <br/>{< br/> System. loadLibrary ("PerformanceTest"); <br/>} catch (UnsatisfiedLinkError ule) <br/>{< br/> System. err. println ("WARNING: cocould not load library! "); <Br/>}< br/> private final String cetcetesttag =" cetcetest: performanceTestFunc :"; <br/>/* JNI test function */<br/> private native void emptyJniFunction (); <br/>/* JAVA test function */<br/> private void emptyJavaFunction () <br/>{</p> <p >}< br/>/** Called when the activity is first created. */<br/> @ Override <br/> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); <br/> setContentView (R. layout. main); <br/> performanceTestFunc (); <br/>}< br/>/* performance test function */<br/> public void performanceTestFunc () <br/> {<br/>/* The test order is 1 million times */<br/> int mLoopCount = 1000000; <br/> int I = 0; <br/> int base = 1000000; <br/> String timeStr_ms; /* String in milliseconds */<br/>/* The precision is in nanoseconds */<br/> long startTime = System. nanoTime (); <br/>/* JNI call */<br/> for (I = 0; I <= mLoopCount; ++ I) <br/>{< br/> emptyJniFunction (); <br/>}< br/> long endTime = System. nanoTime (); <br/> timeStr_ms = String. valueOf (endTime-startTime)/base); <br/> Log. v (PerformanceTestTAG + "In Jni:", timeStr_ms + "ms"); <br/> startTime = System. nanoTime (); <br/>/* JAVA call */<br/> for (I = 0; I <= mLoopCount; ++ I) <br/>{< br/> emptyJavaFunction (); <br/>}< br/> endTime = System. nanoTime (); <br/> timeStr_ms = String. valueOf (endTime-startTime)/base); <br/> Log. v (PerformanceTestTAG + "In JAVA:", timeStr_ms + "ms"); <br/>}< br/>}

Com_jni_performancetest.c

/* Do not edit this file-it is machine generated */<br/> # include <jni. h> <br/>/* Header for class com_jni_PerformanceTest */</p> <p> # ifndef _ alias <br/> # define _ Included_com_jni_PerformanceTest <br/> # ifdef _ _ cplusplus <br/> extern "C" {<br/> # endif <br/>/* <br/> * Class: com_jni_PerformanceTest <br/> * Method: emptyJniFunction <br/> * Signature :() V <br/> */<br/> JNIEXPORT void JNICALL Java_com_jni_PerformanceTest_emptyJniFunction <br/> (JNIEnv * env, jobject obj) <br/>{</p> <p >}< br/> JNIEXPORT jint JNI_OnLoad (JavaVM * vm, void * reserved) <br/>{< br/> JNIEnv * env; <br/> int result; </p> <p> result = JNI_VERSION_1_4; <br/> return result; </p> <p >}< br/> # ifdef _ cplusplus <br/>}< br/> # endif <br/>

Android. mk

Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module: = performancetest

Local_src_files: = src/com_jni_performancetest.c

Include $ (build_shared_library)

 

 

As shown in, JNI calls the empty implementation function of C for 1 million calls, which takes 41747 ms, and Java calls Java's empty implementation function for 1 million times, which takes 25378 Ms. The above tests were performed on Android 1 million, Android 2.2, and jdk1.6.0 _ 17 on the android simulator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.