| d2r diego's weblog |
a simple performance test of java genericsNow that Generics are coming to the Java Platform (in the next JDK), I was curious as to how they would perform, since everything depends in how efficiently they are managing the type management internally (yes, in theory there shouldn't be much difference, but you never know). So I downloaded the Early Access prototype to add generics to JDK 1.4.x, and did a couple of simple tests, with the idea of finding out two things: 1) whether there would be a noticeable performance hit and 2) whether it was necessary to modify JDK 1.4 to run generics-enabled classes. So, I created two simple classes: TestGenerics.java and TestNoGenerics.java (Here is a ZIP file with both Java sources and the compiled classes). Basically the classes do the same thing: they create first a LinkedList and add one million Integer objects to it, and then iterate through each, and then create a Vector and do basically the same. The program measures the time in milliseconds it takes to go from one step to the next, and prints it out. The only difference between the classes is that one uses generics (so that you avoid having to cast the object to an Integer when obtaining it) and the other uses standard Java. First, using generics was as easy as switching javac to the shell file they provide in the kit (with a couple of modifications), and then compiling (there is also a way of using generics by simply using the standard javac with an additional JAR provided in the kit). What's really cool is that once the classes are compiled, it isn't necessary to have the new JAR in the classpath, classes that use generics compile to standard bytecode that can run under JDK 1.4 with no modifications. Regarding the performance, these are the results: Without Generics:
So there is basically no performance hit! This is excellent, specially considering this is an early access release. We get the advantages of static typing on data structures with no performance disadvantages, at least for these simple tests. I am tempted to begin using it right now for spaces... as Homer would say "mmmm.... generics..." :-) Later: the file sizes of the two classes are different, but there's a surprise in store: the class that uses generics is smaller. TestGenerics.class is 1,403 bytes, and TestNoGenerics.class is 1500 bytes. Cool! Categories: technologyPosted by diego on January 16 2003 at 7:58 PM Comments (please see the comments & trackback policy).
Check http://www.nada.kth.se/~snilsson Look for IndexTreeList and LabelTreeList Posted by: Bruno at January 22, 2003 9:49 PMI thought the point was to offer performance enhancements due to a removal of the need to check the types at runtime? The idea of "golly gee! No performance hit!" seems to be missing the point. The point of generating a type-specific container is to provide faster access (no casting/type checking) and accelerate the algorithms operating on those data structures. Posted by: MisterMcSoftware at July 10, 2003 10:28 PMTrue. But I was talking about the early-access prototype, which is what allows us to start using generics (until JDK 15 is released) without worrying that the current JVM, which does *not* know about generics, will create a performance hit because of that. Posted by: Diego at July 10, 2003 10:56 PMI'm not sure what the "point" of generics was really intended to be. It seems that what we'll all enjoy about generics is the reduction in casting clutter when we're using collections. Brett Neumeier pointed out to me that we're really just shifting the casting clutter to the declaration, but that still may be an improvement. I have to say, I'm glad to hear that there doesn't seem to be lots of code bloat or a performance issues. After talking to Brett and some others about it, I'm no longer sure why everybody is so excited about "type safe collections" really .... I don't know about you, but I can't recall if I've ever had a class cast exception while iterating over a collection. I've started thinking that all the buzz over generics is sort of silly. I don't think it will really do much for me, except remove some casting in a few places. That doesn't seem like a big deal to me. Sure, it's a small improvement, but why all the fuss? Posted by: Eric Herman at February 23, 2004 5:50 PMCopyright © Diego Doval 2002-2007.
|
