import java.io.*;
import java.nio.*;
public class BufferPutSpeed {
static PrintStream log = System.out;
static final int N = 100;
static String type(ByteBuffer b) {
return b.isDirect() ? "dir" : "heap";
}
static void go(ByteBuffer src, ByteBuffer dst) {
log.print(type(src) + " --> " + type(dst) + "\t");
long start = System.currentTimeMillis();
for (int i = 0; i < N; i++) {
src.clear();
dst.clear();
dst.put(src);
}
log.println(System.currentTimeMillis() - start);
}
public static void main(String[] args) {
ByteBuffer bbh1 = ByteBuffer.allocate(1 << 20);
ByteBuffer bbh2 = ByteBuffer.allocate(1 << 20);
ByteBuffer bbd1 = ByteBuffer.allocateDirect(1 << 20);
ByteBuffer bbd2 = ByteBuffer.allocateDirect(1 << 20);
go(bbd1, bbd2);
go(bbd1, bbh1);
go(bbh1, bbd1);
go(bbh1, bbh2);
}
}
% jr BufferPutSpeed
dir --> dir 412
dir --> heap 3802
heap --> dir 4360
heap --> heap 411
%
In addition to slowing down pure buffer operations, this discrepancy is also
visible when doing channel reads and writes with non-direct buffers.
-- ###@###.### 2002/4/29