使用 ASCII 行处理 Java IO 的最快方法
我正在通过 Socket 使用 ASCII 输入/输出流,速度至关重要。我听说使用正确的 Java 技术确实会带来不同。我有一本教科书说使用缓冲区是最好的方法,但也建议使用 DataInputStreamReader 进行链接。
对于输出,我使用 BufferedOutputStream 和 OutputStreamWriter ,这似乎很好。但我不确定输入流使用什么。我正在开发新线路,那么扫描仪有什么用处吗?速度至关重要,我需要尽快从网络上获取数据。
谢谢。
PH值
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.
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.
Thanks.
PH
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是为了笑...
使用 LOOPBACK,即仅使用本地进程在我的计算机上运行到本地主机,并且使用适当的“愚蠢”客户端。
您会注意到这会发送 2M“1000 字符”行。这只是一个粗略的吞吐量测试。
在我的机器上,环回,我得到约 190MB/秒的传输速率。字节,而不是位。 190,000 行/秒。
我的观点是,使用骨干 Java 套接字的“简单”方式相当快。这将使任何常见的网络连接饱和(意味着网络将比 I/O 减慢速度)。
可能“足够快”。
您期望什么样的流量?
Just for laughs...
With LOOPBACK, i.e. just running to localhost with local processes, on my machine, and with a suitably "stupid" client.
You'll note that this send 2M "1000 char" lines. It's simply a crude throughput test.
On my machine, loopback, I get ~190MB/sec transfer rate. Bytes, not bits. 190,000 lines/sec.
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).
Likely "fast enough".
What kind of traffic are you expecting?
如果速度绝对重要,请考虑使用 NIO。这是针对完全相同的问题发布的代码示例。
http://lists.apple.com/archives/java- dev/2004/Apr/msg00051.html
编辑:这是另一个示例
http://www.java2s.com/Code/Java/File-Input-Output/UseNIOtoreadatextfile.htm
编辑2:我编写这个微基准测试是为了让您开始测量各种方法的性能。有些人评论说 NIO 不会执行得更快,因为您需要做更多的工作来将数据“整理”成可用的形式,这样您就可以根据您想要做的事情来验证它。当我在我的机器上运行此代码时,对于 45 兆字节的文件,NIO 代码的速度大约快 3 倍;对于 100 兆字节的文件,NIO 代码的速度大约快 5 倍。
If speed is absolutely critical, consider using NIO. Here's a code example posted for the exact same question.
http://lists.apple.com/archives/java-dev/2004/Apr/msg00051.html
EDIT: Here's another example
http://www.java2s.com/Code/Java/File-Input-Output/UseNIOtoreadatextfile.htm
EDIT 2: I wrote this microbenchmark to get you started on measuring the performance of various approaches. Some folks have commented that NIO will not perform faster because you will need to do more work to 'massage' the data into a usable form, so you can validate that based on whatever it is you're trying to do. When I ran this code on my machine, the NIO code was approximately 3 times faster with a 45 megabyte file, and 5 times faster with a 100 megabyte file.
Scanner
用于分隔文本。您没有谈论您的数据是什么样子,所以我无法对此发表评论。如果您只想读取每个换行符,请使用
BufferedReader r = new BufferedReader(new InputStreamReader(Socket.getInputStream()))
和
r.readLine()
当您得到一个空值,你就会知道你已经耗尽了流中的数据。
就速度而言,它们都只是从流中读取数据。因此,假设您不需要扫描仪的额外功能,我认为没有任何特殊的理由使用扫描仪。
A
Scanner
is used for delimited text. You didn't talk about what your data looks like so I can't comment on that.If you just want to read until each newline character, use
BufferedReader r = new BufferedReader(new InputStreamReader(Socket.getInputStream()))
and
r.readLine()
When you get a null value, you will know you have exhausted the data in the stream.
As far as speed is concerned, they are both just reading data out of the stream. So assuming you don't need the extra functionality of a
Scanner
, I don't see any particular reason to use one.我会用 BufferedReader 做一些事情:
根据您的情况,您可以有一个额外的线程来处理“行”中的项目,因为它被填充,但是您需要使用不同的结构来支持集合 - 可以同时使用的集合。
I would do something with a BufferedReader along the lines of:
Depending on your situation you could have an additional thread that processes the items in 'lines' as its getting filled, but then you would need to use a different structure to back the collection- one that can be used concurrently.