java 扫描仪与 InputStreamReader
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10067465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scanner vs InputStreamReader
提问by One Two Three
Does anyone happen to know if there is any difference with regards to performance between the two methods of reading input file below? Thanks.
有没有人碰巧知道下面两种读取输入文件的方法在性能方面是否存在差异?谢谢。
1) Reading a file with Scanner and File
1) 使用 Scanner 和 File 读取文件
Scanner input = new Scanner(new File("foo.txt"));
2) Reading a file with InputStreamReader and FileInputStream
2) 使用 InputStreamReader 和 FileInputStream 读取文件
InputStreamReader input = new InputStreamReader(new FileInputStream("foo.txt"));
采纳答案by Stephen C
The first point is that neitherof those code samples read a file. This may sound fatuous or incorrect, but it is true. What they actually do is open a file for reading. And in terms of what they actually do, there's probably not a huge difference in their respective efficiency.
第一点是这些代码示例都没有读取文件。这听起来可能很愚蠢或不正确,但这是事实。他们实际上做的是打开一个文件进行阅读。就他们实际所做的事情而言,他们各自的效率可能没有太大差异。
When it comes to actually reading the file, the best approach to use will depend on what the file contains, what form the data has to be in for your in-memory algorithms, etc. This will determine whether it is better to use Scanner
or a raw Reader
, from a performance perspective and more importantlyfrom the perspective of making your code reliable and maintainable.
在实际读取文件时,最佳使用方法将取决于文件包含的内容、内存中算法的数据形式等。这将决定是使用更好Scanner
还是raw Reader
,从性能的角度来看,更重要的是从使您的代码可靠和可维护的角度来看。
Finally, the chances are that this won't make a significant difference to the overall performance of your code. What I'm saying is that you are optimizing your application prematurely. You are better of ignoring performance for now and choosing the version that will make the rest of your code simpler. When the application is working, profile it with some representative input data. The profiling will tell you the time is spent reading the file, in absolute terms, and relative to the rest of the application. This will tell you whether it is worth the effortto try to optimize the file reading.
最后,这可能不会对代码的整体性能产生重大影响。我的意思是你过早地优化你的应用程序。您现在最好忽略性能并选择将使其余代码更简单的版本。当应用程序运行时,使用一些有代表性的输入数据对其进行分析。分析将告诉您阅读文件所花费的时间,绝对值,以及相对于应用程序其余部分的时间。这将告诉您尝试优化文件读取是否值得。
The only bit of performance advice I'd give is that character by character reading from an unbuffered input stream or reader is inefficient. If the file needs to be read that way, you should add a BufferedReader to the stack.
我给出的唯一一点性能建议是从无缓冲输入流或读取器逐个字符读取效率低下。如果需要以这种方式读取文件,则应将 BufferedReader 添加到堆栈中。
回答by Andrey Luiz
A difference, and the principal, I guess, is that with the BufferedReader/InputStreamReader you can read the whole document character by character, if you want. With scanner this is no possible. It means that with the InputStreamReader you can have more control about the content of the document. ;)
一个区别,我猜是主要的,使用 BufferedReader/InputStreamReader,如果你愿意,你可以一个字符一个字符地读取整个文档。使用扫描仪这是不可能的。这意味着使用 InputStreamReader 可以更好地控制文档的内容。;)
回答by Kira Chow
In terms of performance, Scanner is definitely the slower one, at least from my experience. It's made for parsing, not reading huge blocks of data. InputStreamReader, with a large enough buffer, can perform on par with BufferedReader, which I remember to be a few times faster than Scanner for reading from a dictionary list. Here's a comparison between BufferedReader and InputStreamReader. Remember that BufferedReader is a few times faster than Scanner.
就性能而言,Scanner 绝对是较慢的,至少从我的经验来看是这样。它用于解析,而不是读取大量数据。InputStreamReader 具有足够大的缓冲区,可以与 BufferedReader 相媲美,我记得在从字典列表中读取时,它比 Scanner 快几倍。这是 BufferedReader 和 InputStreamReader 之间的比较。请记住,BufferedReader 比 Scanner 快几倍。