| ★ wanayoo — archive 1999 http://servlets.com/soapbox/techtips/index.html | Nouvelle recherche | Portail wanayoo |
| ||||||
|
|
||||||
| Tech Tips | ||||||
|
This is my place to share some tech tips about servlets and all things Java.
February 22, 1999 Just for fun: How to do a faster search for primes I was just poking around this afternoon with the PrimeSearcher servlet (Example 3-6) and I realized you can optimize the search code with a minor (but interesting) tweak to the isPrime() method. The code as it appears in the book looks like this (minus comments):
double sqrt = Math.sqrt(candidate);
for (long i = 3; i < sqrt; i += 2) {
if (candidate % i == 0) return false;
}
return true;
Notice how the for loop repeatedly checks if the long variable "i" is less
than the double variable "sqrt". This causes an implicit cast to be
performed on each iteration. Our optimization is to cast the double to a
long ourselves, just once.
long sqrt = (long) Math.sqrt(candidate);The rest of the code remains the same. In a quick test this gave a 20% performance boost to the code, with no loss to readability. Thought some of you might enjoy this trick.
February 17, 1999 A libserver.so port for Linux Thanks to Andrew Gelina and Lari Hotari for making available a libserver.so port for Linux. This library file is a bit of native code that can be used by the Java Web Server to let it listen on ports less than 1024 on Unix -- such as the default web server port 80. Sun provides the source for libserver.so with JWS in the file server.c, but recompiling it isn't always a breeze, so thanks to Andrew and Lari you can download a pre-compiled version here. It's known to work for Linux 2.0.36 (Red Hat 5.2). General instructions for making JWS work on Linux can be found at http://www.blackdown.org/java-linux/products.html.
December 18, 1998 How to make Java Web Server 1.1.3 work on other platforms The httpd.nojre start-up script that comes with JWS is designed to let you run the server with a JVM other than the JRE 1.1.6 provided. This is particularly important for people who aren't using Solaris or Windows (since Sun doesn't provide a JRE for any other platforms). Unfortunately the start-up script is not quite generic enough to really work. I've had success running JWS on IRIX by rewriting the jserv script that's called by httpd.nojre. You can download the revised version here. It differs from the original in that this uses "green" threads and adds rt.jar and i18n.jar to the CLASSPATH. Note that you'll still get error messages on start-up about "libserver.so" not being found. That's OK. That library is only necessary for the server to perform setuid magic to listen on port 80. Without it, the server will listen on port 8080. (If anyone has a compiled version of libserver.so for non-Solaris/non-Linux platforms, let me know and I'll post it here. IRIX would be very nice.)
December 17, 1998 How to get line numbers in your JDK 1.2 stack traces If you've printed a stack trace with JDK 1.2, you've probably noticed that instead of showing you line numbers in the stack it shows "(compiled code)". This is because JDK 1.2 runs with a JIT by default, and as part of the JIT process line numbers are lost. To get your line numbers back you need to run without a JIT. This makes the program execute slower, but helps you find the problem faster. To turn off the JIT:
Type: java -Djava.compiler=NONE ClassToRun
|
||||||
|
Home Book Soapbox Resources Class/Consult About Jason
|
||||||