J
JLM
Hello !
In order to be sure that nio package was more performant than regular
streams, i have tested it with this little piece of code :
// ----------------------------
ByteBuffer loWriteBuffer = ByteBuffer.allocateDirect(1024);
CharBuffer loBuffer = CharBuffer.allocate(512);
Charset charset = Charset.forName("ISO-8859-1");
CharsetEncoder encoder = charset.newEncoder();
long llMillis, llMillis2, llMillis3;
llMillis = System.currentTimeMillis();
try {
FileChannel loChannel = new RandomAccessFile(new
File("C:\\Temp\\Test.txt"), "rw").getChannel();
for (int i = 0; i < 1000; ++i) {
loBuffer.put("ceci est un test\r\n");
loBuffer.flip();
encoder.encode(loBuffer, loWriteBuffer, false);
loWriteBuffer.flip();
loChannel.write(loWriteBuffer);
loBuffer.clear();
loWriteBuffer.clear();
}
loChannel.close();
}
catch (IOException leIO) {
leIO.printStackTrace();
}
llMillis2 = System.currentTimeMillis();
try {
Writer loWriter = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream("C:\\Temp\\Test2.txt"), charset));
for (int i = 0; i < 1000; ++i) {
loWriter.write("ceci est un test\r\n");
}
loWriter.flush();
loWriter.close();
}
catch (IOException leIO) {
leIO.printStackTrace();
}
llMillis3 = System.currentTimeMillis();
System.out.println("Results : nio = " + (llMillis2 - llMillis) + " ms /
standard = " + (llMillis3 - llMillis2) + " ms");
// ----------------------------
I was surprised to see that, on my computer, the regular stream based part
was running about 4 times quicker than the nio based part !!
Here is my question : is my code correctlty optimized ? Anybody already
noticed this performance bottleneck ? Is my test the cause of this problem ?
Thank you !
Jean-Luc
In order to be sure that nio package was more performant than regular
streams, i have tested it with this little piece of code :
// ----------------------------
ByteBuffer loWriteBuffer = ByteBuffer.allocateDirect(1024);
CharBuffer loBuffer = CharBuffer.allocate(512);
Charset charset = Charset.forName("ISO-8859-1");
CharsetEncoder encoder = charset.newEncoder();
long llMillis, llMillis2, llMillis3;
llMillis = System.currentTimeMillis();
try {
FileChannel loChannel = new RandomAccessFile(new
File("C:\\Temp\\Test.txt"), "rw").getChannel();
for (int i = 0; i < 1000; ++i) {
loBuffer.put("ceci est un test\r\n");
loBuffer.flip();
encoder.encode(loBuffer, loWriteBuffer, false);
loWriteBuffer.flip();
loChannel.write(loWriteBuffer);
loBuffer.clear();
loWriteBuffer.clear();
}
loChannel.close();
}
catch (IOException leIO) {
leIO.printStackTrace();
}
llMillis2 = System.currentTimeMillis();
try {
Writer loWriter = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream("C:\\Temp\\Test2.txt"), charset));
for (int i = 0; i < 1000; ++i) {
loWriter.write("ceci est un test\r\n");
}
loWriter.flush();
loWriter.close();
}
catch (IOException leIO) {
leIO.printStackTrace();
}
llMillis3 = System.currentTimeMillis();
System.out.println("Results : nio = " + (llMillis2 - llMillis) + " ms /
standard = " + (llMillis3 - llMillis2) + " ms");
// ----------------------------
I was surprised to see that, on my computer, the regular stream based part
was running about 4 times quicker than the nio based part !!
Here is my question : is my code correctlty optimized ? Anybody already
noticed this performance bottleneck ? Is my test the cause of this problem ?
Thank you !
Jean-Luc