简体   繁体   English

使用BufferedReader.readLine()读取inputStream太慢了

[英]Reading inputStream using BufferedReader.readLine() is too slow

I am using following code. 我正在使用以下代码。

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;

StringBuilder responseData = new StringBuilder();
while((line = in.readLine()) != null) {
    responseData.append(line);
}

But it is taking more than 12 sec to read 200 line. 但阅读200行需要12秒以上。

Please help 请帮忙

I strongly suspect that's because of the network connection or the web server you're talking to - it's not BufferedReader 's fault. 我强烈怀疑这是因为网络连接或你正在与之交谈的网络服务器 - 这不是BufferedReader的错。 Try measuring this: 试着测量一下:

InputStream stream = conn.getInputStream();
byte[] buffer = new byte[1000];
// Start timing
while (stream.read(buffer) > 0)
{
}
// End timing

I think you'll find it's almost exactly the same time as when you're parsing the text. 我想你会发现它几乎与解析文本的时间完全相同。

Note that you should also give InputStreamReader an appropriate encoding - the platform default encoding is almost certainly not what you should be using. 请注意,您还应该为InputStreamReader提供适当的编码 - 平台默认编码几乎肯定不是您应该使用的。

I have a longer test to try. 我有一个更长的测试尝试。 This takes an average of 160 ns to read each line as add it to a List (Which is likely to be what you intended as dropping the newlines is not very useful. 这需要平均160 ns读取每一行,因为它将它添加到List(这可能是你想要的,因为删除换行不是很有用。

public static void main(String... args) throws IOException {
    final int runs = 5 * 1000 * 1000;

    final ServerSocket ss = new ServerSocket(0);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Socket serverConn = ss.accept();
                String line = "Hello World!\n";
                BufferedWriter br = new BufferedWriter(new OutputStreamWriter(serverConn.getOutputStream()));
                for (int count = 0; count < runs; count++)
                    br.write(line);
                serverConn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

    Socket conn = new Socket("localhost", ss.getLocalPort());

    long start = System.nanoTime();
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;

    List<String> responseData = new ArrayList<String>();
    while ((line = in.readLine()) != null) {
        responseData.add(line);
    }
    long time = System.nanoTime() - start;
    System.out.println("Average time to read a line was " + time / runs + " ns.");
    conn.close();
    ss.close();
}

prints 版画

Average time to read a line was 158 ns.

If you want to build a StringBuilder, keeping newlines I would suggets the following approach. 如果你想构建一个StringBuilder,保留换行符,我会建议采用以下方法。

Reader r = new InputStreamReader(conn.getInputStream());
String line;

StringBuilder sb = new StringBuilder();
char[] chars = new char[4*1024];
int len;
while((len = r.read(chars))>=0) {
    sb.append(chars, 0, len);
}

Still prints 还是打印

Average time to read a line was 159 ns.

In both cases, the speed is limited by the sender not the receiver. 在这两种情况下,速度都受发送者而不是接收者的限制。 By optimising the sender, I got this timing down to 105 ns per line. 通过优化发送器,我将此时间降低到每行105 ns。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM