To see if I can really take any benefit of native code (written C) by using JNI (instead of writing complete java application), I want to measure overhead of calling through JNI. What is the best way to measure this overhead?
3 Answers
I wouldn't use a profiler to do quantitative performance testing. Profiling tends to introduce distortions into the actual timing numbers.
I'd create a benchmark that performed one of the actual calculations that you are considering doing in C and compare the C + JNI + Java version against a pure Java version. Be sure that you are comparing apples and apples; i.e. profile and optimize both versions before you benchmark them.
To do the actual benchmarking, I'd construct a loop that performed the calculation a large number of times, record the timings over a large number of iterations and compare. Make sure that you take account of JVM warmup effects; e.g. class loading, JIT compilation and heap warmup.
Like Thihara, I doubt that using C + JNI will help much. And even if it does, you need to take account of the downsides of JNI; e.g. C code portability, platform specific build issues ... and possible JVM hard crashes if your native code has bugs.

- 698,415
- 94
- 811
- 1,216
-
What is JVM warmup effects and heap warmup? – gpuguy Jun 12 '12 at 12:28
-
A warm up effect is where something runs slowly to start with, and speeds up. For example, JVM warmup overheads can include class loading, JIT compilation, static initialization, expanding an initial small heap, etc. – Stephen C Jun 12 '12 at 13:38
Measuring the overhead alone may give you strange results. I'd code a small part of the performance-critical code in both Java and C++ and measure the program performance, e.g., using caliper (microbenchmarking is quite a complicated thing and hardly anybody gets it right).
I would not use any profiler, especially C++ profiler, since the performance of the C++ part alone doesn't matter and since profilers may distort the results.

- 44,714
- 32
- 161
- 320
-
Do I really need to do microbenchmarking for such a performance analysis, especially since you yourself said hardly anybody get it right? Is there any tool that can do the job? – gpuguy Jun 12 '12 at 12:45
-
In case your program runs for a couple of minutes, even a hourglass might do, but I'd suggest to use caliper (linked in my answer), anyway. I believe they got it right (these guys make also Guava). It's quite easy to use and creates good reports, so I'd go for it even when not strictly needed. – maaartinus Jun 12 '12 at 13:28
Use a C++ profiler and a Java profiler. They are available in IDEs for Java. I can only assume in the case of C++ though. And whatever test you design please run through a substantial number of loops to minimize environmental errors.
Oh and do post the results back since I'm also curious to see if there are any improvement in using native code over modern JVMs. Chances are though you won't see a huge performance improvement in native code.

- 7,031
- 2
- 29
- 56