Emmanuel Lecharny wrote: > "이희승 (Trustin Lee) <[EMAIL PROTECTED]>" wrote: >> Hi, >> >> > <snip/> >> I was also able to observe similar test result in JDK 1.5.0.15, but >> running the heap test first always resulted in extremely slow direct >> buffer performance (3x slowdown), which is also interesting. Why >> hotspot so inconsistent like this? >> > > I have tested it with IBM JVM, and I got the very same strange > inconsistency... It would be interesting to profile the test to > understand what really happens internally.
It seems like the hotspot engine optimizes the test(...) method to work
better with a certain type of byte buffer. I inlined the test(...)
method into the two test methods, and now I am getting consistent
result. Direct buffer outperforms heap buffer for all cases in Java 6,
except for the allocation and deallocation. If we can provide
reasonable solution for pooling direct buffers, we will be able to gain
more performance out of MINA.
BTW, it is interesting that direct buffer outperforms heap buffer.
Kudos to the JVM developers!
---- REVISED CODE ----
import java.nio.ByteBuffer;
import org.junit.Test;
public class ByteBufferTest {
@Test
public void heap() {
ByteBuffer buf = ByteBuffer.allocate(2048);
long startTime = System.currentTimeMillis();
for (int i = 1048576; i > 0; i --) {
buf.clear();
while (buf.hasRemaining()) {
buf.getInt(buf.position());
buf.putInt((byte) 0);
}
}
long endTime = System.currentTimeMillis();
System.out.println("heap: " + (endTime - startTime));
}
@Test
public void direct() {
ByteBuffer buf = ByteBuffer.allocateDirect(2048);
long startTime = System.currentTimeMillis();
for (int i = 1048576; i > 0; i --) {
buf.clear();
while (buf.hasRemaining()) {
buf.getInt(buf.position());
buf.putInt((byte) 0);
}
}
long endTime = System.currentTimeMillis();
System.out.println("direct: " + (endTime - startTime));
}
}
--
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/
signature.asc
Description: OpenPGP digital signature
