我尝试了所有三个阅读过程,但无法判断哪个是最好的
内存利用率,处理器使用率,时间复杂度
我在网上看到了很多解决方案,但没有人在上述条款中得出完美的结论。
我已经尝试了一些事情,请检查代码并让我知道如何在上面突出显示的要求中进行更优化。
以下是我的代码。
注意:Out.txt是3Gb文本文件
package Reader;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
/*
* Comparing Execution time of BufferInputReader Vs LineNumberReader Vs
Stream
* o/p > Effeciency of BufferInputReader to LineNumberReader is around ::
200%
*
*/
public class LineReaderBufferInputStream {
public static void main(String args[]) throws IOException {
//LineReaderBufferInputStream
LineReaderBufferInputStream lr = new LineReaderBufferInputStream();
long startTime = System.nanoTime();
int count = lr.countLinesUsingLineNumberReader("D://out.txt");
long endTime = System.nanoTime();
long c1 = (endTime - startTime);
System.out.println(count + " LineReaderBufferInputStream Time taken:: " + c1);
startTime = System.nanoTime();
count = countLinesByBufferIpStream("D://out.txt");
endTime = System.nanoTime();
long c2 = (endTime - startTime);
System.out.println(count + " BufferedInputStream Time taken:: " + c2);
System.out.println("Effeciency of BufferInputReader to LineNumberReader is around :: " + (c1) / c2 * 100 + "%");
// Java8 line by line reader
//read file into stream, try-with-resources
startTime = System.nanoTime();
long cn = countLinesUsingStream("D://out.txt");
endTime = System.nanoTime();
System.out.println(cn +" Using Stream :: " + (endTime - startTime));
}
public int countLinesUsingLineNumberReader(String filename) throws IOException {
LineNumberReader reader = new LineNumberReader(new FileReader(filename));
int cnt = 0;
String lineRead = "";
while ((lineRead = reader.readLine()) != null) {
//if you need to do anything with lineReader.
}
cnt = reader.getLineNumber();
reader.close();
return cnt;
}
public static int countLinesByBufferIpStream(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 1;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
public static long countLinesUsingStream(String fileName) throws IOException{
try (Stream<String> streamReader = Files.lines(Paths.get("D://out.txt"))) {
return streamReader.count();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
一句话:显式传递可移植文件的编码是好的,因为默认编码可能会有所不同。
二进制文件数据到Unicode String的旧默认编码是平台编码。
较新的Files.lines
默认使用UTF-8(hurray)。
这意味着UTF-8转换速度稍慢,并且在错误的非ASCII字符上容易出错,因为UTF-8多字节序列需要正确的位格式。
Files.lines
和其他像Files.newBufferedReader
足够快。使用(缓冲)InputStream / ByteBuffer不转换比转换为文本更快。
Java将(Unicode)文本存储在String中,作为char
的数组,为2字节。最新的java也可以将它存储在单字节编码(jvm选项)中,这可能会节省内存。
可能更好的方法是将文本压缩,例如Out.txt.gz
。交易CPU对磁盘速度。
如果你问这些类中哪一个是最快的或一般使用最少的内存,那么就没有答案。这主要取决于您正在执行的任务。以及如何使用这些课程。
如果您要求以最快的方式计算文件中的行数,那么最快的方法是使用InputStream
直接读入ByteBuffer
,然后计算行终止符。这也将使用最少的内存。
原因如下:
String
的东西都会进行大量不必要的复制,并产生大量垃圾。Reader
的东西都会对字符数据进行解码字节数据。这包括LineNumberReader
BufferedInputStream
并读入大型byte[]
,您实际上正在进行自己的(简单)缓冲。您也可以直接使用InputStream
。read(byte[])
,你正在为byte[]
做一个额外的数据副本。有许多教程可以帮助您了解如何使用ByteBuffer
进行快速I / O.例如:
但是......
在涉及非常大的文件的实际应用程序中,性能瓶颈经常被证明是文件系统和存储设备的性能,或者您处理数据所做的事情......一旦将其存储在内存中。
建议您避免优化应用程序的I / O,直到您具有更高级别的功能,并且能够编写并运行基准测试。然后,您应该分析应用程序以找到瓶颈的真正位置。最后,优化瓶颈。
除非你真的有经验(通常即使你是),你对最佳花费优化工作的直觉往往是不正确的。
最后,计算文件中行的最快方法可能是忘记Java并使用标准的本机代码实用程序;例如在Unix / Linux / MacOS上使用wc pathname
。