<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    I/O Performance

    Although the performance of "old" stream I/O has been improved by implementing it with nio,
    mapped file access tends to be dramatically faster, this program does a  simple performance comparison:
    package think.in.java.io;

    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.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.nio.IntBuffer;
    import java.nio.channels.FileChannel;

    public class MappedIO {
        
    private static int numOfInts = 4000000;
        
    private static int numOfUbuffInts = 200000;
        
        
    private abstract static class Tester{
            
    private String name;
            
    public Tester(String name){
                
    this.name = name;
            }
            
            
    public void runTest(){
                System.out.print(name 
    + "");
                
    try {
                    
    long start = System.nanoTime();
                    test();
                    
    double duration = System.nanoTime() - start;
                    System.out.format(
    "%.2f\n", duration/1.0e9);
                } 
    catch (IOException e) {
                    
    throw new RuntimeException(e);
                }
            }
            
            
    public abstract void test() throws IOException;
        }
        
        
    private static Tester[] tests = {
            
    new Tester("Stream Write") {
                @Override
                
    public void test() throws IOException {
                    DataOutputStream dos 
    = new DataOutputStream(
                            
    new BufferedOutputStream(
                                    
    new FileOutputStream(new File("temp.tmp"))));
                    
    for(int i=0; i<numOfInts; i++){
                        dos.writeInt(i);
                    }
                    dos.close();
                }
            },
            
    new Tester("Mapped Write") {
                @Override
                
    public void test() throws IOException {
                    FileChannel fc 
    = new RandomAccessFile("temp.tmp""rw").getChannel();
                    IntBuffer ib 
    = fc.map(
                            FileChannel.MapMode.READ_WRITE, 
    0, fc.size()).asIntBuffer();
                    
    for(int i=0; i<numOfInts; i++){
                        ib.put(i);
                    }
                    fc.close();
                }
            },
            
    new Tester("Stream Read"){
                @Override
                
    public void test() throws IOException {
                    DataInputStream dis 
    = new DataInputStream(
                            
    new BufferedInputStream(
                            
    new FileInputStream(new File("temp.tmp"))));
                    
    for(int i=0; i<numOfInts; i++){
                        dis.readInt();
                    }
                    dis.close();
                }
            },
            
    new Tester("Mapped Read") {
                @Override
                
    public void test() throws IOException {
                    FileChannel fc 
    = new FileInputStream(new File("temp.tmp")).getChannel();
                    IntBuffer ib 
    = fc.map(
                            FileChannel.MapMode.READ_ONLY, 
    0, fc.size()).asIntBuffer();
                    
    while(ib.hasRemaining()){
                        ib.get();
                    }
                    fc.close();
                }
            }
        };

        
    /**
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
            
    for(Tester test : tests){
                test.runTest();
            }
        }

    }

    // Output:
    Stream Write: 0.73
    Mapped Write: 
    0.06
    Stream Read: 
    0.70
    Mapped Read: 
    0.06

    posted on 2012-11-07 16:00 鹽城小土包 閱讀(158) 評論(0)  編輯  收藏 所屬分類: J2EE

    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲AV日韩综合一区| 亚洲精品日韩中文字幕久久久| 亚洲一级毛片免费在线观看| 免费人成在线观看网站| 国产亚洲成人久久| 日韩久久无码免费毛片软件| 国产精品99久久免费| 亚洲av纯肉无码精品动漫| 日本久久久免费高清| 97视频免费在线| 亚洲AV无码一区二区乱孑伦AS | 九九视频高清视频免费观看 | 亚洲一区二区女搞男| 中文字幕高清免费不卡视频| 亚洲人成影院在线无码按摩店| 国产亚洲精品免费视频播放| 亚洲av无码一区二区三区乱子伦| 性xxxx视频免费播放直播| 久久综合亚洲色HEZYO社区 | 狼友av永久网站免费观看| 在线观看亚洲免费| 浮力影院亚洲国产第一页| 十八禁在线观看视频播放免费| 亚洲久本草在线中文字幕| 四虎精品视频在线永久免费观看| 国产成人亚洲综合网站不卡| 国产免费爽爽视频免费可以看| 免费鲁丝片一级观看| 国产成人 亚洲欧洲| 久久久久亚洲精品无码网址| 国产成年无码久久久免费| 亚洲日产2021三区在线| 在线jlzzjlzz免费播放| 久久av免费天堂小草播放| 亚洲美女人黄网成人女| 最好免费观看韩国+日本| 一区二区三区免费电影| 亚洲人色大成年网站在线观看| 国产网站在线免费观看| 免费黄网站在线观看| 亚洲欧美不卡高清在线|