豪门暖媳txt微盘:JNI性能测试一—JNI调用C与Java调用java性能比较 - zgjxwl-----传说中晨雨的专栏 - CSDN博客

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 01:40:13

JNI性能测试一—JNI调用C与Java调用java性能比较收藏

PerformanceTest.java


view plaincopy to clipboardprint?
  1. package com.jni;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. public class PerformanceTest extends Activity {  
  8.     static  
  9.     {  
  10.         try   
  11.         {  
  12.             System.loadLibrary("PerformanceTest");  
  13.         } catch (UnsatisfiedLinkError ule)   
  14.         {  
  15.             System.err.println("WARNING: Could not load library!");  
  16.         }     
  17.     }  
  18.     private final String PerformanceTestTAG ="PerformanceTest:performanceTestFunc :";  
  19.     /* JNI 测试函数 */  
  20.     private native void emptyJniFunction();  
  21.     /* JAVA测试函数 */  
  22.     private void emptyJavaFunction()  
  23.     {  
  24.           
  25.     }  
  26.     /** Called when the activity is first created. */  
  27.     @Override  
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.main);  
  31.         performanceTestFunc();  
  32.     }  
  33.     /* 性能测试函数 */  
  34.     public void performanceTestFunc()  
  35.     {  
  36.         /* 测试数量级为100万次 */  
  37.         int mLoopCount = 1000000;  
  38.         int i = 0;  
  39.         int base = 1000000;   
  40.         String timeStr_ms;  /* 毫秒对应String*/  
  41.         /* 精确度为纳秒级 */  
  42.         long startTime = System.nanoTime();  
  43.         /* JNI 调用 */  
  44.         for(i = 0;i <= mLoopCount; ++ i)  
  45.         {  
  46.             emptyJniFunction();  
  47.         }  
  48.         long endTime = System.nanoTime();  
  49.         timeStr_ms = String.valueOf((endTime-startTime)/base);  
  50.         Log.v(PerformanceTestTAG+"In Jni:", timeStr_ms+"ms");  
  51.         startTime = System.nanoTime();  
  52.         /* JAVA 调用 */  
  53.         for(i = 0;i <= mLoopCount; ++ i)  
  54.         {  
  55.             emptyJavaFunction();  
  56.         }  
  57.         endTime = System.nanoTime();  
  58.         timeStr_ms = String.valueOf((endTime-startTime)/base);  
  59.         Log.v(PerformanceTestTAG+"In JAVA:", timeStr_ms+"ms");  
  60.     }  
  61. }  


com_jni_PerformanceTest.c


view plaincopy to clipboardprint?
  1. /* DO NOT EDIT THIS FILE - it is machine generated */  
  2. #include   
  3. /* Header for class com_jni_PerformanceTest */  
  4.   
  5. #ifndef _Included_com_jni_PerformanceTest  
  6. #define _Included_com_jni_PerformanceTest  
  7. #ifdef __cplusplus  
  8. extern "C" {  
  9. #endif  
  10. /* 
  11.  * Class:     com_jni_PerformanceTest 
  12.  * Method:    emptyJniFunction 
  13.  * Signature: ()V 
  14.  */  
  15. JNIEXPORT void JNICALL Java_com_jni_PerformanceTest_emptyJniFunction  
  16.   (JNIEnv *env, jobject obj)  
  17. {  
  18.       
  19. }  
  20. JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)  
  21. {  
  22.     JNIEnv *env;  
  23.     int result;  
  24.   
  25.     result = JNI_VERSION_1_4;  
  26.     return result;  
  27.   
  28. }  
  29. #ifdef __cplusplus  
  30. }  
  31. #endif  
  32. #endif  


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)


如上图所示,通过Jni调用c的空实现函数,调用100万次,用时41747ms,而使用java调用java的空实现函数100万次,用时25378ms。以上测试数量级为100万次,系统android 2.2,测试于android模拟器上,jdk1.6.0_17。