JNI数据传输速度测试

记录c++二维数组的json(rapidjson)转换 ,和记录jni通过反射获取java对象数据
测试一:对象传递
测试二:二维对象数组传递

测试一:对象传递

拆解对象成员变量测试

java代码(代码1)
   /**
     * 拆解对象的成员对象
     */
    public void testObjectParam()
    {
        
        long start = System.currentTimeMillis();
        for(int i=0;i<100000;i++)
        {
           Vector3 v1 = new Vector3(1.1f,1.2f,1.3f);
           testObjectParamNative(v1.getX(), v1.getY(), v1.getZ());
        }
        long end = System.currentTimeMillis() - start;
        System.out.println("param use:"+end);
    }
    private native void testObjectParamNative(float x, float y, float z);
c++代码(代码2)
JNIEXPORT void JNICALL Java_com_vking_test_jni_animal_DogJni_testObjectParamNative
(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z)
{
    Vector3 v(x, y, z);
}

对象反射传递测试(耗时)

java代码(代码3)
   /**
     * 传递对象,通过反射获取对象值
     */
    public void testObject()
    {
        
        long start = System.currentTimeMillis();
        for(int i=0;i<100000;i++) 
        {
            Vector3 v1 = new Vector3(1.1f,1.2f,1.3f);
            testObjectNative(v1);
        }
        long use = System.currentTimeMillis() - start;
        System.out.println("Object use:"+use);
    }
    private native void testObjectNative(Vector3 v1);
c++代码(代码4)
jclass clazz;
jfieldID x, y, z;
JNIEXPORT void JNICALL Java_com_vking_test_jni_animal_DogJni_testObjectNative
(JNIEnv *env, jobject obj, jobject vectorJava)
{
    //if (!clazz)
    //{
        //获致obj中对象的class
        clazz = env->FindClass("com/vking/test/other/Vector3");
        // 获取java中字段的ID(最后一个参数是字段的签名)
        x = env->GetFieldID(clazz, "x", "F");
        y = env->GetFieldID(clazz, "y", "F");
        z = env->GetFieldID(clazz, "z", "F");
    //}
    // 获取对应的值
    jfloat xf = env->GetFloatField(vectorJava, x);
    jfloat yf = env->GetFloatField(vectorJava, y);
    jfloat zf = env->GetFloatField(vectorJava, z);
    Vector3 v(xf, yf, zf);
}

结果

拆解变量/ms 反射用法/ms
8 91
7 95
8 96
8 88
7 100

解开代码4中if (!clazz)注释的结果

拆解变量/ms 反射用法/ms
7 5
8 6
8 5
8 5
7 6

通过以上两个结果得知:
(1)反射对象的值传递在提前映射好成员变量的情况下速度与变量的拆解相似
(2)没有提前映射好的反射传递速度比拆解变量慢12倍

测试二:对象的二维数组传递

反射传递

java代码
    public void testArrays()
    {
        Vector3 v1 = new Vector3(1.1f,1.2f,1.3f);
        Vector3 v2 = new Vector3(2.1f,2.2f,2.3f);
        Vector3 v3 = new Vector3(3.1f,3.2f,3.3f);
        Vector3 v12 = new Vector3(1.2f,1.3f,23.0f);
        Vector3 v22 = new Vector3(6.1f,6.2f,5.3f);
        Vector3 v32 = new Vector3(4.1f,7.2f,3.3f);

        
        long start = System.currentTimeMillis();
        for(int i=0;i<100000;i++)
        {
            Vector3[][] v = {{v1, v2, v3},{v12, v22, v32}};
            testArraysNative(v);
        }
        
        long use = System.currentTimeMillis() - start;
        System.out.println("二维数组反射用时:"+use);
        
    }
    private native void testArraysNative(Vector3[][] v1);
c++代码
JNIEXPORT void JNICALL Java_com_vking_test_jni_animal_DogJni_testArraysNative
(JNIEnv *env, jobject obj, jobjectArray objectArrays)
{
    if (!clazz)
    {
        //获致obj中对象的class
        clazz = env->FindClass("com/vking/test/other/Vector3");
        // 获取java中字段的ID(最后一个参数是字段的签名)
        x = env->GetFieldID(clazz, "x", "F");
        y = env->GetFieldID(clazz, "y", "F");
        z = env->GetFieldID(clazz, "z", "F");
    }
    jint nSize = env->GetArrayLength(objectArrays);
    jclass clazz = env->FindClass("com/vking/test/other/Vector3");
    jobject vectorObj;
    jobjectArray objectArray;
    Vector3 veTwo[2][3];
    int i, j;
    for (i = 0; i < nSize; i++)
    {
        objectArray = (jobjectArray)env->GetObjectArrayElement(objectArrays, i);
        jint asize = env->GetArrayLength(objectArray);
        for (j = 0; j < asize; j++)
        {
            vectorObj = env->GetObjectArrayElement(objectArray, j);
            jfloat xf = env->GetFloatField(vectorObj, x);
            jfloat yf = env->GetFloatField(vectorObj, y);
            jfloat zf = env->GetFloatField(vectorObj, z);
            Vector3 v(xf, yf, zf);
            veTwo[i][j] = v;
            //std::cout << v.getX() << "end";
        }
    }
}

二维对象数组传递

java代码
    private static ObjectMapper mapper  = new ObjectMapper();
    public void testArraysJson()
    {
        Vector3 v1 = new Vector3(1.1f,1.2f,1.3f);
        Vector3 v2 = new Vector3(2.1f,2.2f,2.3f);
        Vector3 v3 = new Vector3(3.1f,3.2f,3.3f);
        Vector3 v12 = new Vector3(1.2f,1.3f,23.0f);
        Vector3 v22 = new Vector3(6.1f,6.2f,5.3f);
        Vector3 v32 = new Vector3(4.1f,7.2f,3.3f);

        try
        {

            long start = System.currentTimeMillis();
            for(int i=0;i<100000;i++)
            {
                Vector3[][] v = {{v1, v2, v3},{v12, v22, v32}};
                testArraysJsonNative(mapper.writeValueAsString(v));
            }
            
            long use = System.currentTimeMillis() - start;
            System.out.println("二维数组json用时:"+use);
        } catch (JsonProcessingException e)
        {
            e.printStackTrace();
        }
        
    }
    private native void testArraysJsonNative(String vectorJson);
c++代码
JNIEXPORT void JNICALL Java_com_vking_test_jni_animal_DogJni_testArraysJsonNative
(JNIEnv *env, jobject obj, jstring vectorJson)
{
    
    const char * vectorChar = Tool::jstringToChar(env, vectorJson);
    Document d;
    d.Parse(vectorChar);
    int i = 0, j = 0;
    Vector3 veTwo[2][3];
    for (;i

结果

转json用法/ms 反射用法/ms
2225 81
2219 83
2208 82
2310 81
2212 83

通过结果得知二维对象数组的参数传递中,反射用法速度远快于转json的用法

你可能感兴趣的:(JNI数据传输速度测试)