在内存,处理器,时间方面,BufferInputReader与LineNumberReader和Java之间的最佳文件读取器是什么

问题描述 投票:0回答:2

我尝试了所有三个阅读过程,但无法判断哪个是最好的

内存利用率,处理器使用率,时间复杂度

我在网上看到了很多解决方案,但没有人在上述条款中得出完美的结论。

我已经尝试了一些事情,请检查代码并让我知道如何在上面突出显示的要求中进行更优化。

以下是我的代码。

注意: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;
}

}

java stream filereader bufferedinputstream
2个回答
0
投票

一句话:显式传递可移植文件的编码是好​​的,因为默认编码可能会有所不同。

二进制文件数据到Unicode String的旧默认编码是平台编码。

较新的Files.lines默认使用UTF-8(hurray)。

这意味着UTF-8转换速度稍慢,并且在错误的非ASCII字符上容易出错,因为UTF-8多字节序列需要正确的位格式。

  1. 一般来说,Files.lines和其他像Files.newBufferedReader足够快。
  2. 对于大文件,可以使用ByteBuffer / CharBuffer,即通过FileChannel的内存映射文件。只需在网上搜索。收益不是那么大。

使用(缓冲)InputStream / ByteBuffer不转换比转换为文本更快。

Java将(Unicode)文本存储在String中,作为char的数组,为2字节。最新的java也可以将它存储在单字节编码(jvm选项)中,这可能会节省内存。

可能更好的方法是将文本压缩,例如Out.txt.gz。交易CPU对磁盘速度。


1
投票

如果你问这些类中哪一个是最快的或一般使用最少的内存,那么就没有答案。这主要取决于您正在执行的任务。以及如何使用这些课程。

如果您要求以最快的方式计算文件中的行数,那么最快的方法是使用InputStream直接读入ByteBuffer,然后计算行终止符。这也将使用最少的内存。

原因如下:

  • 任何为每行读取生成String的东西都会进行大量不必要的复制,并产生大量垃圾。
  • 任何使用Reader的东西都会对字符数据进行解码字节数据。这包括LineNumberReader
  • 如果使用BufferedInputStream并读入大型byte[],您实际上正在进行自己的(简单)缓冲。您也可以直接使用InputStream
  • 如果你使用read(byte[]),你正在为byte[]做一个额外的数据副本。

有许多教程可以帮助您了解如何使用ByteBuffer进行快速I / O.例如:


但是......

在涉及非常大的文件的实际应用程序中,性能瓶颈经常被证明是文件系统和存储设备的性能,或者您处理数据所做的事情......一旦将其存储在内存中。

建议您避免优化应用程序的I / O,直到您具有更高级别的功能,并且能够编写并运行基准测试。然后,您应该分析应用程序以找到瓶颈的真正位置。最后,优化瓶颈。

除非你真的有经验(通常即使你是),你对最佳花费优化工作的直觉往往是不正确的。


最后,计算文件中行的最快方法可能是忘记Java并使用标准的本机代码实用程序;例如在Unix / Linux / MacOS上使用wc pathname

© www.soinside.com 2019 - 2024. All rights reserved.