使用ASCII线处理Java IO的最快方法 [英] Fastest way of processing Java IO using ASCII lines

查看:140
本文介绍了使用ASCII线处理Java IO的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Socket使用ASCII输入/输出流,速度至关重要。我听说使用正确的Java技术确实有所作为。我有一本教科书说使用Buffers是最好的方法,但也建议用DataInputStreamReader进行链接。

I'm working with an ASCII input/output stream over a Socket and speed is critical. I've heard using the right Java technique really makes a difference. I have a textbook that says using Buffers is the best way, but also suggests chaining with DataInputStreamReader.

对于输出我使用带有OutputStreamWriter的BufferedOutputStream似乎很好。但我不确定输入流的用途。我正在开发新系列,扫描仪有用吗?速度至关重要,我需要尽快从网络上获取数据。

For output I'm using a BufferedOutputStream with OutputStreamWriter which seems to be fine. But I am unsure on what to use for the input stream. I'm working on new lines, so would Scanner be of any use? Speed is critical, I need to get the data off the network as fast as possible.

谢谢。

PH

推荐答案

只是为了笑......

Just for laughs...

socket = new ServerSocket(2004, 10);
connection = socket.accept();
in = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
String line = null;
do {
    line = br.readLine();
} while (!"done".equals(line));

使用LOOPBACK,即在我的机器上使用本地进程运行到localhost,并且使用适当的愚蠢的客户端。

With LOOPBACK, i.e. just running to localhost with local processes, on my machine, and with a suitably "stupid" client.

requestSocket = new Socket("localhost", 2004);
out = requestSocket.getOutputStream();
PrintWriter pw = new PrintWriter(out);
String line =  "...1000 characters long..."; 
for (int i = 0; i < 2000000 - 1; i++) {
    pw.println(line);
}
line = "done";
pw.println(line);
pw.flush();

你会注意到这发送2M1000 char行。它只是一个粗略的吞吐量测试。

You'll note that this send 2M "1000 char" lines. It's simply a crude throughput test.

在我的机器上,环回,我得到~190MB /秒的传输速率。字节,而不是位。 190,000行/秒。

On my machine, loopback, I get ~190MB/sec transfer rate. Bytes, not bits. 190,000 lines/sec.

我的观点是使用骨库Java套接字的简单方式非常快。这将使任何常见的网络连接饱和(意味着网络将使你的速度比你的I / O慢)。

My point is that the "unsophisticated" way using bone stock Java sockets is quite fast. This will saturate any common network connection (meaning the network will be slowing you down more than your I/O here will).

可能足够快。

您期待什么样的流量?

这篇关于使用ASCII线处理Java IO的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆