Web Hosting Java, JSP, Tomcat 6, J2EE, Servlets, Struts, Jboss
Chapter 8. JNI You cannot dedicate a book
October 31, 2006 on 11:06 pm | In Java | Optimization 46: JNI Surprise We start with a counterintuitive example that will take most Java programmers by surprise. This is the case of a nontrivial task that is executed faster in Java than JNI. A JNI call involves substantial work under the covers to cross from Java to the native environment and back. At the very least we must convert data structures from their JVM representation to the ones expected by C/C++. Take a String, for example. It is represented in Java as a sequence of Unicode characters in which each character is a two-byte entity. A String in C/C++ is a sequence of single-byte characters terminated by a null byte. There’s some work involved in bouncing String objects back and forth across those two environments. Take a concrete task of converting an ASCII String to uppercase. This is definitely faster in C/C++, as you can convert the individual characters in place. In Java, however, we must create a new String object to contain the corresponding uppercase string. The original (lowercase) string remains intact, as String objects in Java are immutable. Given that fact, I was surprised to find out that the Java version was faster than JNI. Both versions were tested with a string of length 128: String s = “a”; for (i = 2; i < 8; i++) {// Build a String of 128 characters s += s; } The measurement consisted of 10,000 iterations of the Java version: String p = s.toUpperCase(); as compared to a corresponding JNI version: String p = jniToUpperCase(s); The C++ implementation of jniToUpperCase() is given by jstring JNICALL Java_Jni_jniToUpperCase(JNIEnv *env, jclass thisClass, jstring s) { char *utf_string; jboolean isCopy; utf_string = (char*) env->GetStringUTFChars(s, &isCopy); for (char *p = utf_string;*p;p++) { *p = toupper(*p); } jstring rs = env->NewStringUTF(utf_string); if (isCopy == JNI_TRUE) { env->ReleaseStringUTFChars(s,utf_string); } return (rs); } The Java version was substantially faster. See Figure 8.1. Figure 8.1. JNI is not an automatic performance winner Page 177
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services
No Comments yet
Sorry, the comment form is closed at this time.
...I
just wanted to take the time to say "Thank you!" for our new webmail
system. It's great! Thanks for taking such good care of us.
Thanks
for helping me out. Just for the record, Webhostingjava.net has been a great
web host! So far your support and handling of questions has far
exceeded that of a "larger web hosting company".
I
would like to thank you for helping me with my domain...You have
shown me great patience and professionalism. I would not hesitate to
recommend you to my clients.