?????嵥 3 ?????????? I/O?????? Byte ?? NIO????????????? NIO ?????????????????????????????? 400 ???????????????д???????????????????
?????嵥 3. I/O ???????????????
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class NIOComparator {
public void IOMethod(String TPATH){
long start = System.currentTimeMillis();
try {
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(new File(TPATH))));
for(int i=0;i<4000000;i++){
dos.writeInt(i);//д?? 4000000 ??????
}
if(dos!=null){
dos.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end - start);
start = System.currentTimeMillis();
try {
DataInputStream dis = new DataInputStream(
new BufferedInputStream(new FileInputStream(new File(TPATH))));
for(int i=0;i<4000000;i++){
dis.readInt();
}
if(dis!=null){
dis.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
end = System.currentTimeMillis();
System.out.println(end - start);
}
public void ByteMethod(String TPATH){
long start = System.currentTimeMillis();
try {
FileOutputStream fout = new FileOutputStream(new File(TPATH));
FileChannel fc = fout.getChannel();//?????????
ByteBuffer byteBuffer = ByteBuffer.allocate(4000000*4);//???? Buffer
for(int i=0;i<4000000;i++){
byteBuffer.put(int2byte(i));//????????????
}
byteBuffer.flip();//???д
fc.write(byteBuffer);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end - start);
start = System.currentTimeMillis();
FileInputStream fin;
try {
fin = new FileInputStream(new File(TPATH));
FileChannel fc = fin.getChannel();//?????????
ByteBuffer byteBuffer = ByteBuffer.allocate(4000000*4);//???? Buffer
fc.read(byteBuffer);//??????????
fc.close();
byteBuffer.flip();//??????????
while(byteBuffer.hasRemaining()){
byte2int(byteBuffer.get()??byteBuffer.get()??byteBuffer.get()??byteBuffer.get());//?? byte ??????
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
end = System.currentTimeMillis();
System.out.println(end - start);
}
public void mapMethod(String TPATH){
long start = System.currentTimeMillis();
//??????????????????
try {
FileChannel fc = new RandomAccessFile(TPATH??"rw").getChannel();
IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE?? 0?? 4000000*4).asIntBuffer();
for(int i=0;i<4000000;i++){
ib.put(i);
}
if(fc!=null){
fc.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end - start);
start = System.currentTimeMillis();
try {
FileChannel fc = new FileInputStream(TPATH).getChannel();
MappedByteBuffer lib = fc.map(FileChannel.MapMode.READ_ONLY?? 0?? fc.size());
lib.asIntBuffer();
while(lib.hasRemaining()){
lib.get();
}
if(fc!=null){
fc.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
end = System.currentTimeMillis();
System.out.println(end - start);
}
public static byte[] int2byte(int res){
byte[] targets = new byte[4];
targets[3] = (byte)(res & 0xff);//??λ
targets[2] = (byte)((res>>8)&0xff);//?ε?λ
targets[1] = (byte)((res>>16)&0xff);//?θ?λ
targets[0] = (byte)((res>>>24));//??λ???????????
return targets;
}
public static int byte2int(byte b1??byte b2??byte b3??byte b4){
return ((b1 & 0xff)<<24)|((b2 & 0xff)<<16)|((b3 & 0xff)<<8)|(b4 & 0xff);
}
public static void main(String[] args){
NIOComparator nio = new NIOComparator();
nio.IOMethod("c:\1.txt");
nio.ByteMethod("c:\2.txt");
nio.ByteMethod("c:\3.txt");
}
}
?????嵥 3 ??????????嵥 4 ?????
?????嵥 4. ???????
????1139
????906
????296
????157
????234
????125
?????????????????嵥 3 ???????????NIO ?? Buffer ??????????????????????????????? DirectBuffer??DirectBuffer ????? ByteBuffer??????????? ByteBuffer ?????????? ByteBuffer ????? JVM ????????????????????????????? DirectBuffer ???????????????????????????????????? ByteBuffer ????????????????????“????????”???м?????????? DirectrBuffer ??????λ??????????“????????”????????? DirectBuffer ??????????????????????????????????????? ByteBuffer ????DirectBuffer ????? ByteBuffer ???????д??????????????????????? DirectrBuffer ???????? ByteBuffer ???DirectBuffer ?? ByteBuffer ???????????嵥 5 ?????
?????嵥 5. DirectBuffer VS ByteBuffer
import java.nio.ByteBuffer;
public class DirectBuffervsByteBuffer {
public void DirectBufferPerform(){
long start = System.currentTimeMillis();
ByteBuffer bb = ByteBuffer.allocateDirect(500);//???? DirectBuffer
for(int i=0;i<100000;i++){
for(int j=0;j<99;j++){
bb.putInt(j);
}
bb.flip();
for(int j=0;j<99;j++){
bb.getInt(j);
}
}
bb.clear();
long end = System.currentTimeMillis();
System.out.println(end-start);
start = System.currentTimeMillis();
for(int i=0;i<20000;i++){
ByteBuffer b = ByteBuffer.allocateDirect(10000);//???? DirectBuffer
}
end = System.currentTimeMillis();
System.out.println(end-start);
}
public void ByteBufferPerform(){
long start = System.currentTimeMillis();
ByteBuffer bb = ByteBuffer.allocate(500);//???? DirectBuffer
for(int i=0;i<100000;i++){
for(int j=0;j<99;j++){
bb.putInt(j);
}
bb.flip();
for(int j=0;j<99;j++){
bb.getInt(j);
}
}
bb.clear();
long end = System.currentTimeMillis();
System.out.println(end-start);
start = System.currentTimeMillis();
for(int i=0;i<20000;i++){
ByteBuffer b = ByteBuffer.allocate(10000);//???? ByteBuffer
}
end = System.currentTimeMillis();
System.out.println(end-start);
}
public static void main(String[] args){
DirectBuffervsByteBuffer db = new DirectBuffervsByteBuffer();
db.ByteBufferPerform();
db.DirectBufferPerform();
}
}
??????????????嵥 6 ?????
?????嵥 6. ???????
????920
????110
????531
????390
???????嵥 6 ?????????????????? DirectBuffer ??????????????????????????ò???-XX:MaxDirectMemorySize=200M –Xmx200M ?? VM Arguments ????????? DirectBuffer ???????????з???????? 200M ????????????????С?????????? 1M??????????????嵥 7 ?????
?????嵥 7. ???д???
????Error occurred during initialization of VM
????Too small initial heap for new size specified
????DirectBuffer ????????????? GC ???棬??? GC ?????????????????????????????? ByteBuffer ??????????????? GC ?????????????????????????? Buffer ??????????????????? DirectBuffer ?????????????????? DirectBuffer???????????? DirectBuffer ???и??????????????????????嵥 8 ????ζ? DirectBuffer ???м?????
?????嵥 8. ?? DirectBuffer ??????
import java.lang.reflect.Field;
public class monDirectBuffer {
public static void main(String[] args){
try {
Class c = Class.forName("java.nio.Bits");//?????????????????
Field maxMemory = c.getDeclaredField("maxMemory");
maxMemory.setAccessible(true);
Field reservedMemory = c.getDeclaredField("reservedMemory");
reservedMemory.setAccessible(true);
synchronized(c){
Long maxMemoryValue = (Long)maxMemory.get(null);
Long reservedMemoryValue = (Long)reservedMemory.get(null);
System.out.println("maxMemoryValue="+maxMemoryValue);
System.out.println("reservedMemoryValue="+reservedMemoryValue);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}