InputStream转String性能测试

本文通过实验对比了Java中使用不同方法从文件中读取数据的性能,包括使用Scanner、直接操作字节流和几种不同的字节流转字符串的方法。结果显示,直接操作字节流的方法效率最高。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static void main(String[] args) {

		try {

			long scannerStart = System.currentTimeMillis();
			for (int i = 1; i < 500000; i++) {
				scannerMethod();
			}

			System.out.println("scanner cost:"
					+ (System.currentTimeMillis() - scannerStart) + " ms");

			long streamStart = System.currentTimeMillis();

			for (int i = 1; i < 500000; i++) {
				streamMethod();
			}
			System.out.println("stream cost:"
					+ (System.currentTimeMillis() - streamStart) + " ms");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public static void scannerMethod() throws MalformedURLException, IOException{
		InputStream in = new URL(
				"file:/C:/Users/forgkan/Desktop/readme.txt")
				.openStream();
		
		try( Scanner s = new Scanner(in) ){
			s.useDelimiter("\\A").next();
		}
		in.close();
	}

	
	public static  void streamMethod() throws IOException{
		InputStream in = new URL(
				"file:/C:/Users/forgkan/Desktop/readme.txt")
				.openStream();
		inputStream2String(in);
		in.close();
	}
	public static String inputStream2String(InputStream in) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i = -1;
		while ((i = in.read()) != -1) {
			baos.write(i);
		}
		return baos.toString();
	}

scanner cost:26661 ms

stream cost:18642 ms


scanner cost:25268 ms

stream cost:17991 ms


public static void main(String[] args) throws MalformedURLException, IOException {
		
		long method1 = System.currentTimeMillis();
		for(int i =1 ;i<50000;i++){
			InputStream in = new URL(
					"file:/C:/Users/forgkan/Desktop/readme.txt")
					.openStream();
			inputStream2String1(in);
			
			in.close();
		}
		System.out.println("method1 cost:"
				+ (System.currentTimeMillis() - method1) + " ms");
		
		long method2 = System.currentTimeMillis();
		for(int i =1 ;i<50000;i++){
			InputStream in = new URL(
					"file:/C:/Users/forgkan/Desktop/readme.txt")
					.openStream();
			inputStream2String2(in);
			
			in.close();
		}
		System.out.println("method2 cost:"
				+ (System.currentTimeMillis() - method2) + " ms");
		
		long method3 = System.currentTimeMillis();
		for(int i =1 ;i<50000;i++){
			InputStream in = new URL(
					"file:/C:/Users/forgkan/Desktop/readme.txt")
					.openStream();
			inputStream2String3(in);
			
			in.close();
		}
		System.out.println("method3 cost:"
				+ (System.currentTimeMillis() - method3) + " ms");
	}

	public static String inputStream2String1(InputStream is) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	public static String inputStream2String2(InputStream in) throws IOException {
		StringBuffer out = new StringBuffer();
		byte[] b = new byte[4096];
		for (int n; (n = in.read(b)) != -1;) {
			out.append(new String(b, 0, n));
		}
		return out.toString();
	}

	public static String inputStream2String3(InputStream is) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i = -1;
		while ((i = is.read()) != -1) {
			baos.write(i);
		}
		return baos.toString();
	}


method1 cost:3086 ms

method2 cost:2056 ms

method3 cost:1813 ms


method1 cost:2986 ms

method2 cost:2045 ms

method3 cost:2156 ms


method1 cost:2921 ms

method2 cost:2047 ms

method3 cost:1795 ms


method1 cost:2980 ms

method2 cost:2015 ms

method3 cost:1744 ms


转载于:https://my.oschina.net/kanlianhui/blog/486514

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值