<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) 評論(0)  編輯  收藏 所屬分類: J2EE

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

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲日韩中文在线精品第一| 亚洲AV日韩AV永久无码免下载| 国产男女猛烈无遮挡免费网站| 久久久久国产成人精品亚洲午夜 | 亚洲AV无码乱码在线观看| 亚洲色成人WWW永久网站| 亚洲人xxx日本人18| 中文字幕一区二区三区免费视频| xxxxwww免费| 亚洲精品456播放| 国产午夜亚洲精品国产| 少妇性饥渴无码A区免费| 热久久精品免费视频| 亚洲一区二区三区首页| a在线视频免费观看在线视频三区| 国产在线观看片a免费观看| 亚洲av无码专区国产乱码在线观看| 亚洲精品久久无码| 嘿嘿嘿视频免费网站在线观看| 亚洲国产成人一区二区精品区| 免费人人潮人人爽一区二区| 蜜臀91精品国产免费观看| 无码色偷偷亚洲国内自拍| 精品久久洲久久久久护士免费| 美女被爆羞羞网站免费| 国产美女精品久久久久久久免费| 无码天堂va亚洲va在线va| 亚洲欧洲美洲无码精品VA| 1000部羞羞禁止免费观看视频 | 和日本免费不卡在线v| 日韩国产欧美亚洲v片| 成人毛片视频免费网站观看| 亚洲专区一路线二| 久久国产免费观看精品3| 久久久久亚洲AV成人无码网站| 我们的2018在线观看免费高清| 国产精品亚洲一区二区三区久久 | 一本久久A久久免费精品不卡| 亚洲黄片手机免费观看| 4399影视免费观看高清直播| 国产午夜亚洲精品不卡免下载|