为什么下面代码的运行结果是使用缓冲流写数据比不使用缓冲流写数据还要慢?

为什么下面代码的运行结果是使用缓冲流写数据比不使用缓冲流写数据还要慢?

package com.imooc;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileDemo3 {

	public static void main(String[] args) {
		byte[] bytes = new byte[100000000];
		for (int i = 0; i < bytes.length;i++) {
			bytes[i] = 'a';
		}
		try {
			FileOutputStream fos = new FileOutputStream("one.txt");
			FileOutputStream fos1 = new FileOutputStream("two.txt");
			BufferedOutputStream bos = new BufferedOutputStream(fos1);
			long startTime = System.currentTimeMillis();
			fos.write(bytes);
			long endTime = System.currentTimeMillis();
			System.out.println("one.txt不使用缓冲流来写用时为:" + (endTime - startTime));
			long startTime1 = System.currentTimeMillis();
			bos.write(bytes);
			bos.flush();
			long endTime1 = System.currentTimeMillis();
			System.out.println("two.txt使用缓冲流来写用时为:" + (endTime1 - startTime1));
			fos.close();
			fos1.close();
			bos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
	

}

http://img1.sycdn.imooc.com//climg/5d53fbf00001d5ee05490133.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕酷酷 2019-08-23 13:04:47

同学你好,代码完成的没有问题~老师将上方代码运行后,可以得到使用缓冲流比不使用缓冲流时间短哦~程序的运行效率与电脑的CPU、网速等因素都有影响的,同学尝试多次运行,

老师的运行结果如下:

http://img1.sycdn.imooc.com//climg/5d5f729e0001a53603710137.jpg

2、同学第二部分的测试代码是没有问题的,同学的理解是正确的,直接写入数组要比写入字符要快一些

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!



weixin_慕勒8187086 2019-08-22 22:11:57
package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestBuffer {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String f1 = "one.txt";
		String f2 = "two.txt";
		String f3 = "three.txt";
		String f4 = "four.txt";
		
		byte[] x = new byte[100000];
		for(int i = 0; i<x.length; i++) {
			x[i] = 'a';
		}
		
		FileOutputStream fos1, fos2a;
		BufferedOutputStream bos1, bos2a;
		long t1, t2;

		// fos time
		try {
			fos1 = new FileOutputStream(f1);
			t1 = System.currentTimeMillis();
			for (int i = 0; i < 100000; i++) {
				fos1.write('a');	
			}
			fos1.close();
			t2 = System.currentTimeMillis();

			System.out.println("fos time: " + (t2 - t1));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		// fos with array time
		try {
			fos2a = new FileOutputStream(f2);
			t1 = System.currentTimeMillis();
			fos2a.write(x,0,x.length);
			fos2a.close();
			t2 = System.currentTimeMillis();
			System.out.println("fos with array time: " + (t2 - t1));

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//bos time
		FileOutputStream fos3;
		try {
			fos3 = new FileOutputStream(f3);
			bos1 = new BufferedOutputStream(fos3);
			t1 = System.currentTimeMillis();
			for (int i = 0; i < 100000; i++) {
				bos1.write('a');
				
			}
			bos1.flush();
			fos3.close();
			bos1.close();	
			t2 = System.currentTimeMillis();
			System.out.println("bos time: " + (t2 - t1));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		// bos with arraty time
		FileOutputStream fos4;
		try {
			fos4 = new FileOutputStream(f4);
			bos2a = new BufferedOutputStream(fos4);
			t1 = System.currentTimeMillis();
			
			bos2a.write(x, 0, x.length);

			bos2a.flush();
			fos4.close();
			bos2a.close();

			t2 = System.currentTimeMillis();
			System.out.println("bos with array time: " + (t2 - t1));
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

不论用不用buffer还是write(int b) 都要比write(byte[] b, int off, int len)慢啊?还是我的测试程序写的有问题?
http://img1.sycdn.imooc.com//climg/5d5ea2280001d1c004010193.jpg

好帮手慕小班 2019-08-15 13:22:00

        同学你好,这里贴出代码中的运行效果不正常是因为同学没有按照方法要求来调用方法:

1、调用write方法时,传入一个byte类型的数组,就需要传入其他两个参数,例如:

write方法:

http://img1.sycdn.imooc.com//climg/5d54ea3b000135b306510421.jpg

对应的代码:

http://img1.sycdn.imooc.com//climg/5d54eb4900019a6809030754.jpg

    2、直接调用write方法,每次写入一个字符,例如

http://img1.sycdn.imooc.com//climg/5d54ebc7000105a409300693.jpg

每次写入一个字符,比直接写入一个数组更快,运行效率更高!并且程序的运行效率与电脑的CPU、网速等因素都有影响,这里多运行几次,还是这个问题吗!

        如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 直接写入数组要比写入字符要快吧?
    2019-08-22 04:22:17
  • 不是吧,我觉得write(int b) 和write(byte[] b, int off, int len) 这两种方法在添加的时候效率应该是每次写一个更快,你可以自己测一下,整体添加的话,还是加上缓冲流的更快!
    2019-08-22 11:40:23
  • 我把测试代码发到回答区了, 测试结果是写入字符要慢。还是测试程序有问题?
    2019-08-22 22:14:11
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师