I never tried JNI(Java Native Interface) before. jni is an api for to call native C code from inside a java code. for a principle I dont like this kind of mixed things. calling java inside c# or calling C# code from C++ is always makes hard codes to understand. anyway I was wondering about the performance of printf between java and jni.
basically there are 2 codes. one is written in java one is written in C. here is our C code
and here is our java code
you can compile C code like this
and you can compile java code like this
after you compile both. you can run the code like this
what it does actually you call Java_HelloWorld_helloFromC function of C from java. and it prints hello world string on console. in my ubuntu it took a while to compile that C code. I needed to install a lot of libraries like this
because gcc was givin this error
after it compiled I run the java and jni and see the output but my intentions was only to investigate to see how fast is jni is compared to C. so what I did is actually run commands in linux like this and see the output
compared to JNI, C is very fast. I was not expecting this difference.65 times faster in C.