2

I am interested in learning JNI. I have developed a simple code to test performance in both java code and c++ code.

here is what I have tried: My Java Code:

public class TestJni 
{
    static
    {
        System.load("../../cpp/dist/testlib.so");
    }

    public static void main(String[] args) 
    {

        int size = 200;
        //////////C++//////////
        long startTime = System.nanoTime();
        new TestJni().doJob(size);
        long endTime = System.nanoTime();

        long duration = (endTime - startTime);

        System.out.println("C++ Run time: "+(duration/1000000)+" milli-second\n");

        ////////////Java///////

        startTime = System.nanoTime();
        javaJob(size);

        endTime = System.nanoTime();
        duration = (endTime - startTime);

        System.out.println("Java Run time: "+(duration/1000000)+" milli-second\n");
    }

    private native void doJob(int size);

    private static void javaJob(int size)
    {
        int f;
        for(int i = 0 ; i<size ; i++)
        {
            for (int j = 0; j < size; j++) 
            {
                for (int k = 0; k < size; k++) 
                {
                    for (int z = 0; z < size; z++) 
                    {
                        f=i+j+k+z;
                    }
                }//for k
            }//for j
        }//for i
    }
}

And This is my C++ Code:

JNIEXPORT void JNICALL Java_test_1jni_TestJni_doJob (JNIEnv * env, jobject obj , jint jsize)
{

    int f;
    int size = jsize;
    for(int i = 0 ; i<size ; i++)
    {
        for (int j = 0; j < size; j++) 
        {
            for (int k = 0; k < size; k++) 
            {
                for (int z = 0; z < size; z++) 
                {
                    f=i+j+k+z;

                }


            }//for k
        }//for j
    }//for i
}

I expected C++ code to be faster or both codes run at same time. ‌But output is completely different:

C++ Run time: 3566 milli-second

Java Run time: 6 milli-second

Why java code is more than 500 times faster than c++ code? I really expected them to run at same time , or c++ code run faster....

Arashdn
  • 691
  • 3
  • 11
  • 25
  • Take a look here http://stackoverflow.com/questions/13973035/what-is-the-quantitative-overhead-of-making-a-jni-call and here http://stackoverflow.com/questions/7699020/what-makes-jni-calls-slow – Michael Laffargue Mar 12 '15 at 16:21
  • @MichaelLaffargue , Well I have only one call in my code. and also there is a huge difference , not a little. – Arashdn Mar 12 '15 at 16:30
  • 3
    could it be that the Java compiler realizes that f is never used and optimizes the code not to do anything at all? You could check with javap. – user2543253 Mar 12 '15 at 17:38
  • @user2543253 that's a good guess. The second question is, did the TO enable C optimization? – Alex Cohn Mar 12 '15 at 19:26
  • @user2543253 , I tested the code and used f every time. java time changed to about 1800 milli-second , still half of c++ code. – Arashdn Mar 13 '15 at 03:48
  • @AlexCohn , What are these optimizations ? – Arashdn Mar 13 '15 at 03:48
  • add `APP_OPTIM=release` to your **Application.mk** – Alex Cohn Mar 13 '15 at 06:06
  • There's an error in your comparison. If you use a static java method, you should use a static native method as well. You are currently timing both instantiation of a TestJNI object, and calling doJob on it in the same timer. You should also pre-allocate startTime and endTime to make the test even more fair - alternatively declare a second startTime and endTime for the second (Java) test. – Nicklas Jensen Mar 14 '15 at 01:47
  • @Nicklas I tried making native function static , and also second start and end time , But nothing changed.... – Arashdn Mar 14 '15 at 15:41
  • To make sure you don't measure initialization stuff you should also run the test methods twice. – user2543253 Mar 16 '15 at 12:07
  • @user2543253 , I double checked every thing ..... all are correct – Arashdn Mar 17 '15 at 04:33
  • Sorry, that was not clear. What I meant is: put each test run twice in your main method. – user2543253 Mar 17 '15 at 18:30

0 Answers0