Link Details

Link 89470 thumbnail
User 111696 avatar

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?)
  • 7
  • 4
  • 1434
  • 535

Comments

Add your comment
User 239201 avatar

amphi replied ago:

-2 votes Vote down Vote up Reply

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.

User 131532 avatar

pbannister replied ago:

1 votes Vote down Vote up Reply

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.)

User 182143 avatar

mezmo replied ago:

0 votes Vote down Vote up Reply

What about using NIO?

User 131532 avatar

pbannister replied ago:

0 votes Vote down Vote up Reply

Benchmarking NIO is another exercise. There is no reason for FileInputStream - with large buffers - to be radically slower than C/C++ using stdio.

User 130040 avatar

mo74395 replied ago:

-1 votes Vote down Vote up Reply

I think there was a good thread some time back about file streams in Java. Anyway, have you tried using BufferedInputStream?

User 131532 avatar

pbannister replied ago:

0 votes Vote down Vote up Reply

Using BufferedInputStream would make this benchmark *slower*. If you write code for a living, you should understand why.

User 161039 avatar

mheath replied ago:

0 votes Vote down Vote up Reply

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.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (7)



Voters Against This Link (4)