In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.