Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of the Reading efficiency of BufferedReader

Shulou Source: shulou.com Published: 2022-06-02 07:15:54 09月14日 Update

This article mainly explains the "BufferedReader reading efficiency problem example analysis", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "BufferedReader reading efficiency problem example analysis"!

The reading efficiency of BufferedReader is 1. General situation

Normal use of readline read, line by line read.

Readline should pay attention to the blocking situation, when the line does not have "/ r", "/ n", "/ r" will block there.

InputStreamReader isr = new InputStreamReader (connection.getInputStream (), "UTF-8"); in = new BufferedReader (isr); String line; while ((line = in.readLine ())! = null) {result + = line;} 2. Use read+CharBufferinputStream = connection.getInputStream (); isr = new InputStreamReader (inputStream, "UTF-8"); in = new BufferedReader (isr); CharBuffer bos = CharBuffer.allocate (20480); StringBuilder builder = new StringBuilder (); while (in.read (bos)! =-1) {bos.flip (); builder.append (bos.toString ()) }

Note: the function of bos.flip () is to point the pointer to the beginning of the buffer

After reading a thousand pieces of data, it is found that the efficiency of using read+CharBuffer is much more efficient than that of readline!

Add:

In the follow-up experiments, it is found that the read method and CharBuffer are not efficient. In fact, it is the frequent creation of String objects that leads to inefficiency, which is solved by using CharBuffer and StringBuilder.

Using BufferedReader to improve efficiency in OJ system

When doing programming problems in the OJ system, if the data read from System.in is very large, the use of Scanner will greatly affect the efficiency and may lead to the final code timeout, so it is best to use BufferedReader to read character data.

For example:

Import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main {public static void main (String [] args) {BufferedReader buf=null; buf=new BufferedReader (new InputStreamReader (System.in)); String str=null; try {int a = Integer.parseInt (buf.readLine ()); double b=Double.parseDouble (buf.readLine ()) } catch (IOException e) {e.printStackTrace ();} System.out.println ();}} Thank you for your reading. The above is the content of "example Analysis of BufferedReader Reading efficiency". After the study of this article, I believe you have a deeper understanding of the problem of BufferedReader reading efficiency, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Efficiency problem analysis situation data learning line content system UTF-8 blocking low frequent code function character object that is beginning train of thought Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS Huawei MySQL Shulou Technology