android 下写文件性能测试

测试了一下,android 下写文件性能:


测试环境 eben T4   android 2.3.4

小文件时RandomAccessFile 比FileOutputStream 快

大文件正好相反FileOutputStream 比 RandomAccessFile 快

我测试

100M , final int length = 1024 * 1024 * 100; 时间ms
RandomAccessFile write:  104857600耗 时:33874
BufferedOutputStream   write: 104857600耗 时:17814

50M
, final int length = 1024 * 1024 * 50; 时间ms
RandomAccessFile write:52428800 耗时:16430
BufferedOutputStream write:52428800  耗时:9497

10M
, final int length = 1024 * 1024 * 10; 时间ms
RandomAccessFile write:10485760 耗时:454
BufferedOutputStream write:10485760  耗时:958


建议开发者存储文件时选择适合的方式。



测试代码如下:

   private void testFileWrite(){
        RandomAccessFile mRandomAccessFile = null;
        File randomFile;
        randomFile = new File("/sdcard/randomFile.test");
          
        final int length = 1024 * 1024 * 10; // *100 //*50
        byte buffer[] = new byte[1024];
        for (int i = 0; i < 1024; ++i) {
             buffer[i] = (byte) (0xFF & i);
        }

        long start_time = System.currentTimeMillis();
         try {
              mRandomAccessFile = new RandomAccessFile(randomFile, "rw");
            mRandomAccessFile.setLength(length);
            for (int i = 0, max = length / 1024; i < max; ++i){
                mRandomAccessFile.write(buffer, 0, 1024);
            }
              if (mRandomAccessFile != null) {
                mRandomAccessFile.close();
            }
        } catch (IOException e) {
             e.printStackTrace();
        }
        long end_time = System.currentTimeMillis();
        Log.d("", "RandomAccessFile write:" + length +" 耗时:"+(end_time-start_time));
        start_time  = System.currentTimeMillis();
  /
        FileOutputStream downFileWriter = null;
        BufferedOutputStream downFileBuffer = null;
        File streamFile = new File("/sdcard/streamFile.test");
      
        try {
            downFileWriter = new FileOutputStream(streamFile, true);
            downFileBuffer = new BufferedOutputStream(downFileWriter,102400);
            for (int i = 0, max = length / 1024; i < max; ++i){
                downFileBuffer.write(buffer, 0, 1024);
            }

            if (null != downFileBuffer) {
                downFileBuffer.flush();
                downFileBuffer.close();
                downFileBuffer = null;
            }   
            if (null != downFileWriter){
                downFileWriter.flush();

                downFileWriter.close();

                downFileWriter = null;

            }
        } catch (Exception e) {
               
            e.printStackTrace();
        }
       
        end_time = System.currentTimeMillis();
         Log.d("", "BufferedOutputStream write:" + length +"  耗时:"+(end_time-start_time));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值