2008-03-07
关于ByteBuffer用于读写文件的一个粗略测试。
测试环境:双核1.6G,内存2G,jdk1.5.0_13,windows xp,用于测试的文件大小为40728KB.
1.ByteBuffer
测试代码1
2.direct ByteBuffer
测试代码2
3.普通的IO
测试代码3
分别的测试结果为(单位:ms)
ByteBuffer
1.719
2.656
3.640
4.657
direct ByteBuffer
1.859
2.593
3.610
4.640
平常IO.
1.625
2.594
3.531
4.562
当缓冲大小由原来的1024设置为10240时的测试结果为(单位:ms)
ByteBuffer
1.562
2.484
3.531
4.500
direct ByteBuffer
1.453
2.484
3.469
4.453
平常IO.
1.438
2.453
3.610
4.531
1.ByteBuffer
测试代码1
FileInputStream fis=new FileInputStream("D:/NetworkWideAgent.war");
FileOutputStream fos=new FileOutputStream("D:/NetworkWideAgent1.war");
long start=System.currentTimeMillis();
FileChannel inChannel=fis.getChannel();
FileChannel outChannel=fos.getChannel();
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
while (true)
{
byteBuffer.clear();
int cnt=inChannel.read(byteBuffer);
if(cnt<0) break;
byteBuffer.flip();
outChannel.write(byteBuffer);
}
System.out.println("task time="+(System.currentTimeMillis()-start));
fis.close();
fos.close();
2.direct ByteBuffer
测试代码2
FileInputStream fis=new FileInputStream("D:/NetworkWideAgent.war");
FileOutputStream fos=new FileOutputStream("D:/NetworkWideAgent4.war");
long start=System.currentTimeMillis();
FileChannel inChannel=fis.getChannel();
FileChannel outChannel=fos.getChannel();
ByteBuffer byteBuffer=ByteBuffer.allocateDirect(1024);
while (true)
{
byteBuffer.clear();
int cnt=inChannel.read(byteBuffer);
if(cnt<0) break;
byteBuffer.flip();
outChannel.write(byteBuffer);
}
System.out.println("task time="+(System.currentTimeMillis()-start));
fis.close();
fos.close();
3.普通的IO
测试代码3
FileInputStream fis=new FileInputStream("D:/NetworkWideAgent.war");
FileOutputStream fos=new FileOutputStream("D:/NetworkWideAgent4.war");
long start=System.currentTimeMillis();
byte[] buffer=new byte[1024];
while (true)
{
int cnt=fis.read(buffer);
if(cnt<0) break;
fos.write(buffer,0,cnt);
}
System.out.println("task time="+(System.currentTimeMillis()-start));
fis.close();
fos.close();
分别的测试结果为(单位:ms)
ByteBuffer
1.719
2.656
3.640
4.657
direct ByteBuffer
1.859
2.593
3.610
4.640
平常IO.
1.625
2.594
3.531
4.562
当缓冲大小由原来的1024设置为10240时的测试结果为(单位:ms)
ByteBuffer
1.562
2.484
3.531
4.500
direct ByteBuffer
1.453
2.484
3.469
4.453
平常IO.
1.438
2.453
3.610
4.531







评论排行榜