<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 鹽城小土包 閱讀(154) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 青柠影视在线观看免费高清 | 久久精品国产亚洲综合色| a毛片在线免费观看| 91大神亚洲影视在线| 国产精品黄页在线播放免费| 国产高潮久久免费观看| 亚洲国产av一区二区三区丶| 亚洲国产午夜中文字幕精品黄网站| 无码精品人妻一区二区三区免费看 | 亚洲中文无码mv| 国产av无码专区亚洲av果冻传媒 | 国产精品免费观看久久| 国产VA免费精品高清在线| 91亚洲国产成人久久精品网址| 免费在线观看中文字幕| 国产成人精品久久免费动漫| 青青草97国产精品免费观看| 亚洲综合激情视频| 亚洲伊人久久精品影院| 成人男女网18免费视频| 久久免费区一区二区三波多野| 相泽南亚洲一区二区在线播放| 久久亚洲日韩精品一区二区三区| 免费国产a国产片高清| 久久久久久精品免费看SSS | 免费jjzz在线播放国产| 1000部拍拍拍18免费网站| h视频在线免费观看| 亚洲av永久无码天堂网| 亚洲天堂电影在线观看| 亚洲国产成人高清在线观看| 国产a级特黄的片子视频免费| 亚洲一级毛片免费观看| 久久福利青草精品资源站免费| 人人鲁免费播放视频人人香蕉| 亚洲一线产区二线产区区| 亚洲福利电影一区二区?| 国产成人亚洲综合色影视 | 综合偷自拍亚洲乱中文字幕| 亚洲va在线va天堂va手机| 久久精品国产亚洲夜色AV网站|