1

I am new to JNI (or Native Language programming) and would appreciate any help in this. Here's what I am trying to do and along with the execution times.

I am trying to find sum of the array elements of doubles of size 100,000,000 and trying to get JNI to get it faster.

For Java (Pure Java), it takes 154ms For C (Pure C), it takes 0 ms For JNI (Java calls Native C), it takes 800 ms

I understand that JNI would have its own performance overhead but I take the time it takes to execute JNIEXPORT jdouble JNICALL Java_Sample1_sum only 1 ms (i.e. after the control enters and before it leaves this sum method), so my question is is JNI so slow that it takes about 799 ms to call the JNI itself i.e. the following block takes 800 ms

start = System.currentTimeMillis(); sample.sum(doubleArray); end = System.currentTimeMillis();

I can share my entire code, if this doesn't help in answering. Also, I have searched about optimizing JNI.

Vamsi Nadella
  • 19
  • 1
  • 3
  • 2
    First of all see: https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – lexicore Apr 13 '18 at 19:01
  • 4
    you got zero ms to sum an array of doubles in C? I can hardly believe in that... – Leonardo Alves Machado Apr 13 '18 at 19:01
  • 2
    Then see: https://stackoverflow.com/questions/13973035/what-is-the-quantitative-overhead-of-making-a-jni-call – lexicore Apr 13 '18 at 19:02
  • 3
    Finally, why do you care about what looks like a few nanoseconds per iteration? If plain Java can sum 100 million doubles in 154ms (I don't believe the C result) there's no point in investing any time to "optimize" this. Please research "premature optimization" and understand that this particular operation is likely to be immaterial in the context of your entire application. – Jim Garrison Apr 13 '18 at 19:09
  • @JimGarrison Absolutely. Few nanoseconds for the price of carrying native libraries around? Isn't worth it. – lexicore Apr 13 '18 at 19:21

1 Answers1

1

As lexicore suggested:

  1. Check how to create a proper benchmark for your java solution here.
  2. Then follow and implement the bench-marking solutions found in the first answer here.

As a side note and from personal experience - there are many important factors involved in making the decision of using a JNI solution over Java, or over an IPC solution with a C/C++ side process.

Example considerations:

  1. How many invocations of the API are expected?
  2. Is this legacy code that should be refactored or a new solution?
  3. Is this an actual, time critical transaction that should be optimized?
  4. Is the solution expected to change much, or is it a once go effort?

Do not make the JNI or IPC C/C++ choice lightly. There is a great overhead associated here.

And do not forget that Java's just-in-time compilation can work wonders with multiple method invocations (I bench marked this vs c/c++ and was amazed at java's long term, amortized speed vs c/c++).

Good Luck!

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42