By bloid
via bannister.us
Published: Jun 24 2008 / 10:55
On my first crack at a Java-based Wide Finder implementation, got a surprisingly slow result for using java.io.FileInputStream to read a file. Dug into the sources to track down the cause.
(Is there a better public-reference to the OpenJDK code than here?)
Comments
amphi replied ago:
Even if there is a native method... it doesn't mean that it's necessarily used. Like there are native methods for lots of Math stuff... those /aren't/ used. The VM (well, Sun's VM) inlines those calls directly.
The author also forgot to include native numbers for comparison. His test doesn't run all that fast, but there is no indication that it could run faster than that.
pbannister replied ago:
The Java compiler/JVM cannot inline native code. That would require inlining the readBytes() behavior - not feasible.
Inlining Math operations is quite different. (You should have got the "why" of this in school.)
mezmo replied ago:
What about using NIO?
pbannister replied ago:
Benchmarking NIO is another exercise. There is no reason for FileInputStream - with large buffers - to be radically slower than C/C++ using stdio.
mo74395 replied ago:
I think there was a good thread some time back about file streams in Java. Anyway, have you tried using BufferedInputStream?
pbannister replied ago:
Using BufferedInputStream would make this benchmark *slower*. If you write code for a living, you should understand why.
mheath replied ago:
Here is the JNI documentation for SetByteArrayRegion http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp22933
I can't help but wonder why GetByteArrayElements() is not being used. You shouldn't have to allocate a buffer at all since you already have the byte array.
Voters For This Link (7)
Voters Against This Link (4)