Java IO 性能测试


原文出处:http://javasam.iteye.com/blog/1488644


  1. import java.io.BufferedInputStream;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.RandomAccessFile;  
  10. import java.nio.IntBuffer;  
  11. import java.nio.channels.FileChannel;  
  12.   
  13.   
  14. public class compareMappedAndStream {  
  15.   
  16.     private static int numOfInts = 4000000;  
  17.     private static int numOfUbuffInts = 2000000;  
  18.   
  19.     private abstract static class Tester {  
  20.         private String name;  
  21.   
  22.         public Tester(String name) {  
  23.             this.name = name;  
  24.         }  
  25.   
  26.         public void runTest() {  
  27.             System.out.print(name + ": ");  
  28.             try {  
  29.                 long start = System.nanoTime();//  
  30.                 test();  
  31.                 double duration = System.nanoTime() - start;  
  32.                 System.out.format("%.2f\n", duration / 1.0e9);  
  33.             } catch (IOException e) {  
  34.                 throw new RuntimeException(e);  
  35.             }  
  36.         }  
  37.   
  38.         public abstract void test() throws IOException;  
  39.     }  
  40.   
  41.     /** 
  42.      * 测试各种流的效率 
  43.      */  
  44.     private static Tester[] tests = {  
  45.     /** 
  46.      * 测试Stream写的效率 
  47.      */  
  48.     new Tester("Stream Write") {  
  49.         @Override  
  50.         public void test() throws IOException {  
  51.             DataOutputStream dos = new DataOutputStream(  
  52.                     new BufferedOutputStream(new FileOutputStream(new File(  
  53.                             "temp.tmp"))));  
  54.             for (int i = 0; i < numOfInts; i++) {  
  55.                 dos.writeInt(i);  
  56.             }  
  57.             dos.close();  
  58.         }  
  59.     },  
  60.     /** 
  61.      * 测试内存映射写的效率 
  62.      */  
  63.     new Tester("Mapped Write") {  
  64.         @Override  
  65.         public void test() throws IOException {  
  66.             FileChannel fc = new RandomAccessFile("temp.tmp""rw")  
  67.                     .getChannel();  
  68.             IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size())  
  69.                     .asIntBuffer();  
  70.             for (int i = 0; i < numOfInts; i++) {  
  71.                 ib.put(i);  
  72.             }  
  73.             fc.close();  
  74.         }  
  75.     },  
  76.     /** 
  77.      * 测试Steam读的效率 
  78.      */  
  79.     new Tester("Stream Read") {  
  80.         @Override  
  81.         public void test() throws IOException {  
  82.             DataInputStream dis = new DataInputStream(new BufferedInputStream(  
  83.                     new FileInputStream("temp.tmp")));  
  84.             for (int i = 0; i < numOfInts; i++) {  
  85.                 dis.readInt();  
  86.             }  
  87.             dis.close();  
  88.         }  
  89.     },   
  90.     /** 
  91.      * 测试Mapped读的效率 
  92.      */  
  93.     new Tester("Mapped Read") {  
  94.         @Override  
  95.         public void test() throws IOException {  
  96.             FileChannel fc = new FileInputStream(new File("temp.tmp")).getChannel();  
  97.             IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY,0,fc.size()).asIntBuffer();  
  98.             while(ib.hasRemaining()){  
  99.                 ib.get();  
  100.             }  
  101.             fc.close();  
  102.         }  
  103.   
  104.     },  
  105.     new Tester("Stream Read/Write"){  
  106.   
  107.         @Override  
  108.         public void test() throws IOException {  
  109.             RandomAccessFile raf = new RandomAccessFile(new File("temp.tmp"), "rw");  
  110.             raf.writeInt(1);  
  111.             for(int i = 0 ; i < numOfUbuffInts ; i ++){  
  112.                 raf.seek(raf.length() - 4);  
  113.                 raf.writeInt(raf.readInt());  
  114.             }  
  115.             raf.close();  
  116.         }},  
  117.     new Tester("Mapped Read/Write"){  
  118.         @Override  
  119.         public void test() throws IOException {  
  120.             FileChannel fc = new RandomAccessFile("temp.tmp""rw").getChannel();  
  121.             IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE,0,fc.size()).asIntBuffer();  
  122.             ib.put(0);  
  123.             for(int i = 1 ; i < numOfUbuffInts ; i ++){  
  124.                 ib.put(ib.get(i -1));  
  125.             }  
  126.             fc.close();  
  127.         }  
  128.               
  129.         }  
  130.   
  131.     };  
  132.     /** 
  133.      * 测试 
  134.      * @param args 
  135.      */  
  136.     public static void main(String[] args) {  
  137.         for(Tester tester : tests){  
  138.             tester.runTest();  
  139.         }  
  140.     }  
  141. }  
  142. 运行结果:  
  143. Stream Write: 0.46  
  144. Mapped Write: 0.04  
  145. Stream Read: 0.46  
  146. Mapped Read: 0.04  
  147. Stream Read/Write: 43.14  
  148. Mapped Read/Write: 0.04  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值