测试环境:双核1.6G,内存2G,jdk1.5.0_13,windows xp,用于测试的文件大小为40728KB.
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
评论
发表评论

您还没有登录,请登录后发表评论

zhanglm
搜索本博客
博客分类
最近加入圈子
最新评论